【发布时间】:2011-05-28 14:07:37
【问题描述】:
我的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]。
【问题讨论】:
-
请附上验证码
标签: ruby-on-rails validation ruby-on-rails-3 field-with-errors