activerecord - rails accepts_nested_attributes_for :reject_if not working -
I turned on to try to override the auto save
parameter because I do not think so Can be done
I moved order
to shipping address
model has_shipping_address
and now I have it:
#model .. class orders & lt; ActiveRecord :: Base is_to: billing_address belongs_to: shipping_address ac_secures_nested_attributes_for: billing_ address acceptations_nested_attributes_for: shipping_address ,: reject_if = & gt; Proc {| Properties | | Features ["has_shipping_address"]! = '1'} DEF after_initialize self.build_billing_address until billing_address self.build_shipping_address shipping_address addresses end end class shipping & lt; Order eder attr_accessor: has_shipping_address end class order adapter & lt; ActiveRecord :: Base validates_presence_of: name # More assumptions here .. see end # & lt;% form_for @ order do | F | & Gt%; # ... for & lt;% f.fields_: shipping_address do | Addr_f | & Gt%; & Lt;% = addr_f.check_box: has_shipping_address% & gt; & Lt;% = addr_f.text_field: name% & gt; # More fields for addresses .. <% end%> & Lt;% end% & gt;
The problem is that : reject_if
does not work in doing this. It does not matter that the value of has_shipping_address
, the nested < Code> Shipping address is still called the Save
method, which results in verification errors.
Am I doing something wrong here? It's getting a little disappointing.
turns on: reject
did not work because I nested The callback of the shipping_address
was generated by the after_initialize
order. After looking at it (or helpful method) to see it, it works as expected.
def after_initialize self.build_billing_address Until the billing_address end # see now & lt;% form_for @order do | F | & Gt%; # ... & lt;% @ order.build_shipping_address Unless the @ command is there. Shipping_address% & gt; For & lt;% f.fields_: shipping_address do | Addr_f | & Gt%; & Lt;% = addr_f.check_box: has_shipping_address% & gt; & Lt;% = addr_f.text_field: name% & gt; # More fields for addresses .. <% end%> & Lt;% end% & gt;
I hope at least this will help anyone else as it has been very disappointing for me.
Comments
Post a Comment