Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Created October 3, 2010 04:08
Show Gist options
  • Save BinaryMuse/608256 to your computer and use it in GitHub Desktop.
Save BinaryMuse/608256 to your computer and use it in GitHub Desktop.
lib/core_ext/rails/active_model/errors.rb - get one validation error message per invalid attribute
module ActiveModel
class Errors
def full_message_per_field
messages_per_field = []
handled_attributes = []
each do |attribute, messages|
next if handled_attributes.include? attribute
messages = Array.wrap(messages)
next if messages.empty?
if attribute == :base
messages_per_field << messages.first
else
attr_name = attribute.to_s.gsub('.', '_').humanize
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
options = { :default => "%{attribute} %{message}", :attribute => attr_name }
messages_per_field << I18n.t(:"errors.format", options.merge(:message => messages.first))
end
handled_attributes << attribute
end
messages_per_field
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment