Hi, I have a “Customer” model and a “Product” model, where a Customer
“has many” Products. When editing a Customer, I also want to display the
Products that person has at the bottom of the page, available for
editing, (“complex forms” as described in Railscast 73, 74, and 75).
at the end of Customer’s edit.html.erb:
…
<%= render :partial => ‘product’, :collection => @customer.products,
:locals => { :f => f } %>
in _product.html.erb:
<% fields_for “customer[product_attributes][]”, product do
|product_form| %>
%>
%>
Overall, this scheme is working fine, and I can edit any of the Products
from the Customer page and update them with no problem. The only
problem comes when I try to edit the “Payment Received” check box…it
appears to be submitting “1” and “0” when I check the box and update it.
As a result, a Customer who originally had 3 Products will, after
checking the “Payment Received” check box in some of them, have 5 (or 6
or 7) Products afterwards.
The (messed up) params sent to the Update after checking the boxes are
as follows:
(rdb:27) params[:customer][:product_attributes][0]
{“remarks”=>“remark1.”, “product_id”=>“72”, “company_id”=>“1”,
“subscription”=>"
1", “serial_number”=>“4444”}
(rdb:27) params[:customer][:product_attributes][1]
{“payment_received”=>“1”, “subscription”=>“0”}
(rdb:27) params[:customer][:product_attributes][2]
{“remarks”=>"", “product_id”=>“73”, “payment_received”=>“0”, “id”=>“22”,
“company_id”=>“3”,
“subscription”=>“1”, “broadcast_period(1i)”=>“2008”,
“broadcast_period(2i)”=>“3”
, “serial_number”=>“55555”, “broadcast_period(3i)”=>“31”}
(rdb:27) params[:customer][:product_attributes][3]
{“payment_received”=>“1”, “subscription”=>“0”}
(rdb:27) params[:customer][:product_attributes][4]
{“remarks”=>“remark3.”, “product_id”=>“73”, “payment_received”=>“0”,
“id”=>“23”, "company_id
"=>“4”, “subscription”=>“1”, “broadcast_period(1i)”=>“2008”,
“broadcast_period(2
i)”=>“3”, “serial_number”=>“3333”, “broadcast_period(3i)”=>“31”}
(rdb:27) params[:customer][:product_attributes][5]
{“payment_received”=>“1”, “subscription”=>“0”}
(rdb:27) params[:customer][:product_attributes][6]
{“payment_received”=>“0”, “id”=>“24”,
“broadcast_period(1i)”=>“2008”,“broadcast_period(2i)”
=>“3”, “broadcast_period(3i)”=>“31”}
Sorry its a little hard to read, but there should only be 3 Products
with this Customer, but there is now 7. The “payment_received” (and
“subscription”) params were checked, and should be “1” but they both
alternate between “1” and “0”, creating extra Products.
Any idea what is causing this? Everything else (text_field, text_area,
select, date_select) all work fine, and my form check_boxes work fine
elsewhere. The Generated HTML appears to be fine for my check boxes,
looks the same as the others… Is it because I’m updating a model
within a model?
Any insight would be greatly appreciated, thank you.