Validators message in rails 3

In Rails 3, validators are changed: now you can specify all validators for a specific field once:

so instead write

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

Now I can write
 
Rails Style 3

 validates :username,  :length => { :minimum => 5, :maximum => 40 }

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

thank

+3
source share
1 answer

When using the shortened method, validates :modelyou can add only certain messages in the context of a specific validator. Example:

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

, :length. Rails , .

+1

Source: https://habr.com/ru/post/1785642/


All Articles