Here is my code. The first commented line works fine; the second doesn't. The documentation is sketchy here - no mention of the :message option under the "validates" method, but "validates_format_of" says :message is fine. (http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates) What is going on here?
class Product < ActiveRecord::Base
validates :title, :description, :image_url, :presence => true
#validates :title, :length => {:minimum => 10, :message => "help!"}
#validates :title, :length => {:minimum => 10}, :message => "help!"
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :image_url, :format => {
:with => %r{\.(gif|jpg|png)$}i,
:message => 'must be a URL for GIF, JPG or PNG image.'
}
end
In the first commented line, the message value is an option to the :length parameter and it appears its being ignored. To test try
validates :title, :length => {:minimum => 10, :foobar => "help!"}
and you will find it to works without creating an error.
validates is just a shortcut to default validators. You seem to want to set the validates_format_of :message options, so I think you want something like:
validates :title, length => {:minimum => 10}, :format => { :message => "help!" }
Length validations are baked in:
validates_length_of :title, :minimum => 10, :message => "help!"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.