<%= form_for list do |f| %> <!-- This might be an issue in production mode -->
<%= f.fields_for l = list.list_items.build, index: l.id do |list_item| %>
<td><%= list_item.check_box :user_item_id, { checked: false, include_hidden: false }, item.id, "nil" %></td> <!-- checkboxes seem problematic, try check_box_tag? -->
<td><%= item.name %></td>
<td>$<%= number_with_precision(item.price, :precision => 2) %></td>
<td><%= list_item.number_field :quantity,
min: 1,
class: "num"%></td>
<% end %>
<% end %>
If I have three of these on my page and all three are checked, Rails will send an extra unchecked value for each form. Why is this?
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sn2HLFvVLX7ZFxBTHvRGz25H+bbyiVl8E8Kok5J7788q8WA33U8RpWmLXjU2GTjD2ybW7RMaiX1CwLJ6oCLXmQ==", "list"=>{"list_item"=>[{"user_item_id"=>"nil"}, {"user_item_id"=>"19", "quantity"=>"1"}, {"user_item_id"=>"nil"}, {"user_item_id"=>"20", "quantity"=>"2"}, {"user_item_id"=>"nil"}, {"user_item_id"=>"21", "quantity"=>"3"}], "name"=>""}, "commit"=>"Create List"}
However if I turn hidden fields off, I get other issues with my forms "stealing" each-other's data when a preceding box is left unchecked. For example here I left the first form unchecked:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qszyk+q+pJ77WuDh2/K2S8lTuBD99DptwqeEdDI4U+4yQBWIbCSYRUvGrofzH8hHfDKXSxxn6myTpZ6dAGFruA==", "list"=>{"list_item"=>[{"quantity"=>"1", "user_item_id"=>"20"}, {"quantity"=>"2", "user_item_id"=>"21"}, {"quantity"=>"3"}], "name"=>""}, "commit"=>"Create List"}
Notice how the first unchecked form neglected to set the user_item_id to "nil", and instead left the param out completely and stole the next form's user_item_id, thus messing up my quantity values (which I set to the same as before).
Why is this?
PS: I originally had "nil" just nil, but doing so prevents the unchecked value from being passed in any case and I was having issues with the forms stealing values from eachother like I posted above. If anyone could tell me how to make it so that an unchecked box means that my fields_for will not be submitted at all that would be great.
Aucun commentaire:
Enregistrer un commentaire