Rails 3, Validates, Custom Message

In Rails < 3, I could have a validation on model such as:

validates_presence_of :name, :message => “some custom message here”

If I try something similar in Rails 3:

validates :name, :presence => true , :message => “some other custom
message”

obviously, it throws an exception. So the question is, in Rails 3, how
to you give a simple custom message? Do I need to go through and set up
a custom validation class for each validation, just to show a different
message? I’ve looked at:

http://www.railsapi.com/doc/rails-v3.0.0/classes/ActiveModel/Validations/ClassMethods.html#M003721

but it seems to show that a custom class would need to be set up for
each attribute of my models? I’m sure there’s a shortcut, but have yet
to find it.

Thanks.

v

On Sun, Oct 3, 2010 at 12:07 PM, John T. [email protected] wrote:

In Rails < 3, I could have a validation on model such as:

validates_presence_of :name, :message => “some custom message here”

If I try something similar in Rails 3:

validates :name, :presence => true , :message => “some other custom
message”

validates :name, :presence => {“some other custom”}

most options are now passed like that , for example

validates :name, :unique => {:scope => :company_id}

you see, options are now passed as hashes to the validation key

radhames brito wrote:

validates :name, :presence => {“some other custom”}

most options are now passed like that , for example

validates :name, :unique => {:scope => :company_id}

you see, options are now passed as hashes to the validation key

Yeah, I did see that, but I don’t see any options for what I want to do.
The syntax you gave, however throws a syntax error.

Hello John,

In Rails 3 also validates_presence_of is present. Why you are not using
this
one?

validates_presence_of :name, :message => “some custom message here”

Thanks!
Butu

Butu wrote:

Hello John,

In Rails 3 also validates_presence_of is present. Why you are not using
this
one?

validates_presence_of :name, :message => “some custom message here”

Thanks!
Butu

Hi,

I am aware the original version is available, but I was trying to update
to the new validators in Rails 3. I guess I’m going to just have to
ditch that and go back to the old. Seems to be another “New and Shiny”
way of doing things, but lose simple functionality… :wink:

Hello.

Try this:

validates :name, :presence => { :message => “My custom message” }

On Sun, Oct 3, 2010 at 10:27 PM, John T. [email protected] wrote:

you see, options are now passed as hashes to the validation key

Yeah, I did see that, but I don’t see any options for what I want to do.
The syntax you gave, however throws a syntax error.

validates :name, :presence => { :message => “hello world” }

what is it you want to do ?

Radhames Brito wrote:

On Sun, Oct 3, 2010 at 10:27 PM, John T. [email protected] wrote:

you see, options are now passed as hashes to the validation key

Yeah, I did see that, but I don’t see any options for what I want to do.
The syntax you gave, however throws a syntax error.

validates :name, :presence => { :message => “hello world” }

what is it you want to do ?

Ahhhh… I didn’t have quite the right syntax. That seems to work. I’m
just trying to give a custom error message if an attribute is blank,
besides the normal “can’t be blank”.

Thanks!