:message with new style validates?

I found some questions posted in this regard in the past, but I
couldn’t find anything conclusive. Hence this post, with two
questions. This is in context of Rails 3.x.

  1. Is it recommended (as a matter of “style”) to use the
    “validates :name, :presence => true” instead of more verbose
    “validates_presence_of :name”? (RailsGuides promotes the latter, I
    think)
  2. If it is the former, how do I specify custom messages when a
    particular validation fails? Any examples? What if I need to have
    different messages for different criteria (e.g. with :presence and
    with :format)?

Thanks in advance,
Kedar

On 19 Mar 2011, at 18:40, Kedar M. [email protected]
wrote:

I found some questions posted in this regard in the past, but I
couldn’t find anything conclusive. Hence this post, with two
questions. This is in context of Rails 3.x.

  1. If it is the former, how do I specify custom messages when a
    particular validation fails? Any examples? What if I need to have
    different messages for different criteria (e.g. with :presence and
    with :format)?

Instead of :presence => true you should be able to say :presence =>
{…}

and specify options for that validation (such as the message) in the
hash

Fred

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