Rails: Custom validation message

2020-02-05 02:12发布

问题:

I'm trying to make a simple custom validation message. The validation I'm using compiles and runs fine, but I don't see any change in the message:

validates :rating, :inclusion => { :in => 0..5 }, :presence => { :message => " must be within 0-5" }

The message I get is still Rating is not included in the list

I need to validate that rating is present and is a decimal between 0-5

回答1:

Alright, I solved it. This is the validation that works:

validates :rating, :inclusion => { :in => 0..5, :message => " should be between 0 to 5" }
validates :rating, :presence => { :message => " cannot be blank" }

and I added this

validates :rating, :numericality => { :message => " should be a number" }