Ruby-on-rails Rails:自定义验证消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6966263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:43:51  来源:igfitidea点击:

Rails: Custom validation message

ruby-on-railsruby-on-rails-3validationactiverecord

提问by Ankit Soni

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" }

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

我收到的消息还在 Rating is not included in the list

I need to validate that ratingis present and is a decimalbetween 0-5

我需要验证它rating存在并且decimal介于两者之间0-5

回答by Ankit Soni

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" }

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