【发布时间】:2012-05-13 15:29:30
【问题描述】:
我遇到了验证问题,如果我使用以下语法,一切都很好(没有失败)。
validates :title, uniqueness: true
但是,如果我将其更改为此,我会失败。
validates :title, uniqueness: {message: 'Title must be unique'}
这里是完整性测试:
test "product is not valid without a unique title " do
product = Product.new( title: products(:ruby).title,
description: "yyy",
price: 1,
image_url: "fred.gif" )
assert !product.save
assert_equal "has already been taken", product.errors['title'].join('; ')
end
我有一个固定装置,可以为 Ruby 产品等添加书名。
据我了解,这两个验证应该是相同的,只是一个提供了自定义错误消息。这是我在使用自定义消息时遇到的错误。
1) 失败: test_product_is_not_valid_without_a_unique_title_(ProductTest) 预期但已 .
提前感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails ruby validation