我正在使用
validates :feed_id, presence: true, uniqueness: true
我应该如何生成自定义错误消息以指定用户已经订阅了此提要(feed_id)字段重复
我知道我可以只做 validate_uniqueness_of 但它会不必要地使代码困惑。如果唯一性验证失败,我该如何传递特定的错误消息??
最佳答案
使用键 message 和期望的消息作为值而不是 true 放置一个散列:
validates :feed_id, presence: true, uniqueness: {message: "already subscribed"}
关于ruby-on-rails - rails 3 : how to generate custom error message from failed validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10488302/