Rails:自定义验证消息

时间:2011-08-06 10:44:22

标签: ruby-on-rails ruby-on-rails-3 validation activerecord

我正在尝试制作一个简单的自定义验证消息。我正在使用的验证编译并运行正常,但我没有看到消息有任何变化:

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

我得到的消息仍为Rating is not included in the list

我需要验证rating是否存在且decimal之间是0-5

1 个答案:

答案 0 :(得分:24)

好吧,我解决了。这是有效的验证:

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

我添加了这个

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