Skip to content

Instantly share code, notes, and snippets.

@morgoth
Created December 1, 2009 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morgoth/246206 to your computer and use it in GitHub Desktop.
Save morgoth/246206 to your computer and use it in GitHub Desktop.
# Semantic error messages for is used with formtastic and it will generate error messages for attributes you
# are passing including base.
# You can use it like this:
# - semantic_form_for (...)
# = semantic_error_messages_for @model, :attribute, :another_one
def semantic_error_messages_for(object, *methods)
full_errors = methods.inject([]) do |array, method|
attribute = object.class.human_attribute_name(method)
errors = Array(object.errors.on(method)).to_sentence
errors.present? ? array << [attribute, errors].join(" ") : array ||= []
end
full_errors << object.errors.on_base
full_errors.flatten!
full_errors.compact!
return nil if full_errors.blank?
content_tag(:fieldset, :class => "errors") do
content_tag(:ol) do
full_errors.map { |error| content_tag(:li, error) }.join
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment