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| %> <tr><th>Serial Number</th> <td><%= product_form.text_field :serial_number, :index => nil %></td> </tr> ... <tr><th>Payment_received</th> <td><%= product_form.check_box :payment_received, :index => nil %></td> </tr> ... 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Mar-26 07:38 UTC
Re: Check_box submits both "1" and "0" when checked
On 26 Mar 2008, at 03:03, Brent Smith wrote:> > 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? >I''m not entirely sure what is going on, but this bit of information might be helpful. By default if you have just a checkbox and it''s not checked then the browser submits absolutely nothing. Things would be somewhat simpler if it sent false and true in the other case (or 0 and 1 or whatever). To get around this when you use check_box (but not check_box_tag) rails creates a hidden input in addition to the checkbox, the input input is wired up to send 0 and the checkbox will send nothing or 1. It''s also true that parameter names are very significant to rails: if you have 2 inputs called bob, then the first one wins (this is what should happen with the checkboxes). However if you have 2 inputs called bob[] then you''ll get an array called bob in your params. I suspect something to do with these 2 things is happening. Fred> Any insight would be greatly appreciated, thank you. > -- > Posted via http://www.ruby-forum.com/. > > --~--~---------~--~----~------------~-------~--~----~ > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > For more optio--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks a lot, that looks like it''s probably the case in this situation... I''ll check it out. Brent -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---