This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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