Rails: form_for checkbox set to true or false whether the box is checked/unchecked Rails: form_for checkbox set to true or false whether the box is checked/unchecked ruby ruby

Rails: form_for checkbox set to true or false whether the box is checked/unchecked


Rails will do this for you when your form is acting on an object, just leave all the extra stuff off the tag like so:

<div class="field">    <%= f.label "Get Email" %> <br />    <%= f.check_box :send_email %> <br /></div>

And it should all start working as you expect. The checkboxes will be ticked if the attribute is true, and vice versa the checked state when you submit the form will affect the attribute. Rest of your code is fine.


Further informations about forms and updating database

Indeed, the last answer is right : using the form_for syntax is enough and Rails will do the association unchecked:false / checked:true for you.

<div class="field">    <%= f.label "Get Email" %> <br />    <%= f.check_box :send_email %> <br /></div>

I had the same problem even with that syntax. The fact is the server's console returned me Unpermitted parameter: checkbox_value : don't forget to update your required/permitted parameters to put into params ! And in my case :

# ***_controller.rb      private        def operator_params          params.require(:operator).permit(:name, :website, :checkbox_value, :global)        end


I had same problem.I did

<% @batches.each do |batch| %>  <div class="name_list<%=cycle('odd', 'even')%>"><li>    <label><%= check_box_tag "send_sms[batch_ids][]",  batch.id,false,:class=>'right' %>   <div class="att_list_names"> <%= batch.full_name %></div> </label>   </li>  </div>  <% end %>