我的 Product
类有 price
字段,它在数据库的 Products
表中有一个适当的列,并且 new_shop
帮助字段(定义为 attr_accessor
,并且在数据库的 Products
表中没有合适的列)。
当 price
验证失败时,输入字段被包裹在 field_with_errors
div 中,但是当 new_shop
验证失败时,它不是用 field_with_errors
div 包装。为什么?
这是为这些输入字段生成的 HTML:
<input type="text" name="product[price]" id="product_price">
<input type="text" value="" name="product[new_shop]" id="product_new_shop">
更多信息:
class Product < ActiveRecord::Base
attr_accessor :new_shop
accepts_nested_attributes_for :shop
validates_presence_of :price
...
end
class Shop < ActiveRecord::Base
validates_presence_of :name
...
end
提交表单时,new_shop
值将传递给产品的 shop_attributes[:name]
。
最佳答案
所以实际上验证失败的是 :name 属性?这就是为什么 new_shop 没有得到 fieldWithErrors div 的原因:这会查看 @product.errors 以逐个字段地决定它是否有错误。即
#comes to do the :new_shop field
#looks to see if @product.errors.on(:new_shop) is not blank
#if it isn't blank, wraps the error div round it.
关于ruby-on-rails - rails 3 : Why the field with error is not wrapped with "field_with_errors" div when validation fails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4495539/