Class: ActiveModel::Errors
- Inherits:
-
Object
- Object
- ActiveModel::Errors
- Defined in:
- lib/belorussian/active_model_ext/custom_error_message.rb
Instance Method Summary collapse
-
#full_messages ⇒ Object
Redefine the ActiveModel::Errors.full_messages method: Returns all the full error messages in an array.
Instance Method Details
#full_messages ⇒ Object
Redefine the ActiveModel::Errors.full_messages method:
Returns all the full error messages in an array. 'Base' messages are handled as usual.
Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
in which case the attribute name is omitted.
E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
Переопределяет метод ActiveModel::Errors.full_messages. Сообщения об ошибках для атрибутов теперь не имеют префикса с названием атрибута если в сообщении об ошибке первым символом указан “^”.
Так, например,
validates_acceptance_of :accepted_terms, :message => 'нужно принять соглашение'
даст сообщение
Accepted terms нужно принять соглашение
однако,
validates_acceptance_of :accepted_terms, :message => '^Нужно принять соглашение'
даст сообщение
Нужно принять соглашение
Returns all the full error messages in an array.
class Company
validates_presence_of :name, :address, :email
validates_length_of :name, :in => 5..30
end
company = Company.create(:address => '123 First St.')
company.errors. # =>
["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/belorussian/active_model_ext/custom_error_message.rb', line 42 def = [] each do |attribute, | = Array.wrap() next if .empty? if attribute == :base .each {|m| << m } else attr_name = attribute.to_s.gsub('.', '_').humanize attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) = { :attribute => attr_name, :default => "%{attribute} %{message}" } .each do |m| if m =~ /^\^/ << m[1..-1] else << I18n.t(:"errors.format", .merge(:message => m)) end end end end end |