开发者

Validators message in rails 3

In Rails 3, the validators is changed: now is possible to specify all the validators for a specific field in once:

so instead to write

Rails 2.x.x style

validates_size_of :username, :within => 5..15, :message=> "username size must be between 5 and 15"

now I can write

Rails 3 style

 validates :username,  :length => { :minimum开发者_运维技巧 => 5, :maximum => 40 }

But if I add :messge=> "bla bla bla" in this last example (Rails 3 style) an error occur, so the question is: How to edit personal error message to the model in order to show them in the view ?

Thank you


When you use the shorthand validates :model method you can only add specific messages within the context of a specific validator. Example:

validates :username, :length => { :minimum => 5, :maximum => 40, :message => 'should be between 5 and 40 characters' }

Note that the message is in the hash for the :length key. Otherwise Rails doesn't know which validator the message should be applied to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜