How to use validates?

I can’t get this to work.

class User < ActiveRecord::Base

validates :firstname, :presence => true, :message => “First name is
missing”

end

And no error message. Why?

On Jan 28, 2011, at 9:54 AM, Paul B. wrote:

I can’t get this to work.

class User < ActiveRecord::Base

validates :firstname, :presence => true, :message => “First name is
missing”

end

And no error message. Why?

What do your views look like, either at the layout level or the users/
new|edit level? If you don’t put some sort of view code in place, you
won’t ever see the errors if they’re being set. Are you able to see
other errors, and just not that one?

Walter

It’s more of a basic question. How do I get that single error, in the
simples way? @model.errors…something

On 28/01/11 15:54, Paul B. wrote:

Did you mean:
validates :firstname, :presence => {:message => “First name is missing”}
?


best regards
Bente P.

Bente P. wrote in post #978180:

On 28/01/11 15:54, Paul B. wrote:

Did you mean:
validates :firstname, :presence => {:message => “First name is missing”}
?


best regards
Bente P.

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?

On 28/01/11 15:54, Paul B. wrote:

PS: have you defined to show error_messages in your view?
http://railscasts.com/episodes/211-validations-in-rails-3
or use the plugin dynamic_form and the old <%= f.error_messages
%>-Helper in your form


best regards
Bente P.

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 B. wrote:

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 P.

Bente P. wrote in post #978687:

Is this what you need?


best regards
Bente P.

Yes! I’ve used some of this without luck but I probably did something
wrong. With your help, your code, I’m sure I’ll get it right. Thank you
very much. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs