Ruby on rails 当多个字段的验证失败时,显示相应的错误消息
我在我的Ruby on rails 当多个字段的验证失败时,显示相应的错误消息,ruby-on-rails,validation,Ruby On Rails,Validation,我在我的产品模型上设置了一个验证规则,以检查名称和数量这两个字段是否唯一。例如,您可以 奥利奥12包 奥利奥100包 两者都是有效条目。但是,你不能有两张奥利奥100块装的唱片 我使用了这个问题中描述的方法来设置规则(),但是默认的错误消息只是突出显示第一个字段,并说它已经被使用了,这并不能准确地向试图插入产品信息的用户描述问题 如果这是在多个字段上验证唯一性的通用Rails解决方案,那么如何根据失败的规则适当设置验证消息 如果有其他解决方案会自动显示相应的错误消息,那么会是什么?有两种方法
产品
模型上设置了一个验证规则,以检查名称和数量这两个字段是否唯一。例如,您可以
- 奥利奥12包
- 奥利奥100包
如果有其他解决方案会自动显示相应的错误消息,那么会是什么?有两种方法。假设您的模型设置如下:
class Product < ActiveRecord::Base
...
validates_uniqueness_of :quantity, scope: :name
...
end
validates_uniqueness_of :quantity, scope: :name, message: "Product and quantity are not unique. And, Oreo's are delicious."
validates_uniqueness_of :quantity, scope: :name, message: "Product and quantity are not unique. And, Oreo's are delicious."