Ruby on Rails Monday, January 31, 2011

I think, I still missunderstand you. But perhaps this is, what you need:

You want to iterate through errors:

@user.errors.each do |attr, message|
  do something with attr and message
end

If you want an array of all full_messages, use:
@user.errors.full_messages
(how they are handling it in this episode: http://railscasts.com/episodes/211-validations-in-rails-3)

@user.errors.generate_message(:firstname)

Or if you need the other methods of @user.errors, look here:
http://api.rubyonrails.org/
ActiveModel::Errors

Is this what you need?


PS:

validates :firstname, :presence => true, :message => "First name is missing" 
I think this is confused mix of old
validates_presence_of :firstname, :message => message
and newer sexy validations
validates :attribute, :presence => options_hash/true/array/

if you look in the source:
75 validations = defaults.slice!(:if, :unless, :on, :allow_blank, :allow_nil)  only these 5 options are possible global-options. :message is not one of them. So I am sure, your way won't do what you want it to do. 




On 28/01/11 17:03, Paul Bergstrom wrote:
Bente Pieck wrote in post #978180: 
On 28/01/11 15:54, Paul Bergstrom wrote: 
Did you mean: validates :firstname, :presence => {:message => "First name is missing"} ?   -- best regards Bente Pieck 
Yes. But I've also seen alidates :firstname, :presence => true, :message  => "First name is missing"  However that's not my problem. How do I get hold of the error message?  


--  best regards Bente Pieck

No comments:

Post a Comment