我的Product班级有price字段,它在数据库的表中具有适当的列Products,以及new_shop辅助字段(定义为attr_accessor,并且在数据库的表中没有适当的列Products)。
当验证price失败时,输入字段被field_with_errorsdiv包裹,但当验证new_shop失败时,它不被field_with_errorsdiv包裹。为什么 ?
以下是为这些输入字段生成的 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].