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

I have a model called users that has 2 boolean attributes send_email and send_text. I have a form that edits the User model, and I want it to set those attributes to true/false depending on whether the box is check/unchecked. Here is my form

<%= form_for(@user) do |f| %> <div> <%= f.label :email %> <br /> <%= f.text_area :email %> <br /> </div> <div> <%= f.label :cell %> <br /> <%= f.text_area :cell %> <br /> </div> <div> <%= f.label "Get Email" %> <br /> <%= f.check_box :send_email, {}, true, false %> <br /> </div> <div> <%= f.label "Get Text" %> <br /> <%= f.check_box :send_text, {}, true, false %> <br /> </div> <div> <%= f.submit "Submit", class: "button small radius" %> <%= link_to "go back", @user, class: "button small radius secondary" %> </div> <% end %> 

And here is the update action of the user_controller

def update @user = User.find(params[:id]) @user.update_attributes(params[:user]) redirect_to @user end 

The form and update looks like it works perfectly, but when i submit this form with the send_email or send_text box checked, it does not change the attributes of the user model (send_email,send_text) to false

2

4 Answers

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> <%= 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.

0

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> <%= 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><li> <label><%= check_box_tag "send_sms[batch_ids][]", batch.id,false,:class=>'right' %> <div> <%= batch.full_name %></div> </label> </li> </div> <% end %> 

I had a similar problem. I solved it first by adding a teacher role to users and made boolean and set it as default:false.

rails generate migration add_teacher_to_users teacher:boolean 

then created a registration as:

<div> <%= form.label "Tutor/Teacher? Tick" %> <%= form.check_box :teacher, class: "form-control", placeholder: "Tick if you are a Tutor" %> </div> 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like