mercredi 18 février 2015

Rails - Params missing when updating unchecked checkboxes (as nil values)


Vote count:

0




I've a problem with some checkboxes in a form, in that when they're submitted unchecked, I get the error param is missing or the value is empty: user.


I can't see why it's doing this; in most cases, this problem arises from not having the blank boxes defined in a hidden field, which I've got.


It's worth noting, the checkboxes work perfectly when something is checked.


Here's the checkbox code (@user is the standard User.find(params[:id])):



<%= form_for(@user) do |f| %>
<h2>Add your dietary requirements:</h2>
<div class="field">
<% hidden_field_tag "user[dietary_requirement_ids][]", nil %> <!-- a lack of this field often seems to generate this problem -->
<% DietaryRequirement.all.each do |dietary_requirement| %>
<%= check_box_tag "user[dietary_requirement_ids][]", dietary_requirement.id, @user.dietary_requirement_ids.include?(dietary_requirement.id), id: dom_id(dietary_requirement) %>
<%= label_tag dom_id(dietary_requirement), dietary_requirement.name %><br>
<% end %>
</div>
<%= f.submit "Update your account", class: "btn btn-primary" %>
<% end %>


When something is checked, the params correctly pass through like this:



Parameters: {"user"=>{"dietary_requirement_ids"=>["2"]}, "commit"=>"Update your account", "id"=>"2"}


Though when nothing is selected, they incorrectly submit as follows:



Parameters: {"commit"=>"Update your account", "id"=>"2"}


The frustrating thing is, these checkboxes have worked perfectly for me to date, and today began to throw the error - with no discernible changes that should have affected this.


I've had a little look and can't find a solution to my specific problem out there - any one able to help?!


Thanks all, Steve.



asked 1 min ago







Rails - Params missing when updating unchecked checkboxes (as nil values)

Aucun commentaire:

Enregistrer un commentaire