Rails forms - When no checkboxes are checked, param is not included in POST Rails forms - When no checkboxes are checked, param is not included in POST ruby-on-rails ruby-on-rails

Rails forms - When no checkboxes are checked, param is not included in POST


Add hidden field above all checkboxes with empty value. It will be sent in case user didn't check any checkboxes.

<%= form_for @task do |f| %>  <%= hidden_field_tag "task[objective_ids][]", nil %>  <% Objective.all.each do |objective| %>    <%= check_box_tag :objective_ids, objective.id, @task.objectives.include?(objective), :name => 'task[objective_ids][]' %>  <% end %>  <%= f.button :submit %><% end %>

Here is a good railscasts about this.
You may also want to check the source code for it.