【发布时间】:2009-06-24 10:14:35
【问题描述】:
我放弃了尝试覆盖autosave 参数,因为我认为它无法完成。
我将 has_shipping_address 从 Order 移至 ShippingAddress 模型,现在我有了:
#the models..
class Order < ActiveRecord::Base
belongs_to :billing_address
belongs_to :shipping_address
accepts_nested_attributes_for :billing_address
accepts_nested_attributes_for :shipping_address, :reject_if => proc { |attributes| attributes["has_shipping_address"] != '1' }
def after_initialize
self.build_billing_address unless billing_address
self.build_shipping_address unless shipping_address
end
end
class ShippingAddress < OrderAddress
attr_accessor :has_shipping_address
end
class OrderAddress < ActiveRecord::Base
validates_presence_of :name
#more validations here..
end
#the view
<% form_for @order do |f| %>
#...
<% f.fields_for :shipping_address do |addr_f| %>
<%= addr_f.check_box :has_shipping_address %>
<%= addr_f.text_field :name %>
#more fields for the address..
<% end %>
<% end %>
问题是:reject_if 似乎没有完成它的工作。不管has_shipping_address的值是多少,在嵌套的ShippingAddress上仍然调用save方法,导致验证错误。
我在这里做错了吗?这有点令人沮丧。
【问题讨论】:
标签: ruby-on-rails activerecord nested nested-forms