Bruno
Bruno

Reputation: 6449

Checkbox parameters submits but does not INSERT

In my Users table I have boolean columns: owner, manager. The below does not update the table once the form is submitted.

  <%= f.check_box :owner %> 
  <%= f.label :owner %>

Logs:

Processing by UsersController#create as HTML


Parameters: {"utf8"=>"✓", "authenticity_token"=>"k0m814s9fRjCUZxeBXn5GO3o5Fq0evZG1Xc7IfUCOYU=", "user"=>{"name"=>"Bob Dylan", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "manager"=>"0", "owner"=>"1"}, "commit"=>"Create my account"}
Unpermitted parameters: manager, owner
   (0.2ms)  begin transaction
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
  SQL (56.3ms)  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "remember_token", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", Thu, 06 Jun 2013 14:57:53 UTC +00:00], ["email", "[email protected]"], ["name", "Bob Dylan"], ["password_digest", "$2a$10$t5hux4e.jDWS9GH7fJj7Z.gSkLehpJxzfwXOTqbnL6LA7zWZT/11S"], ["remember_token", "J-OFaVfx3a4KMGQ0Q9vttg"], ["updated_at", Thu, 06 Jun 2013 14:57:53 UTC +00:00]]

Upvotes: 0

Views: 148

Answers (3)

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10198

In your User controller you must have something like:

def user_params
  params.require(:user).permit(:manager, :owner)
end

Upvotes: 1

Julian G.
Julian G.

Reputation: 143

You will need to go into your view and create either a checkbox or a radio button for that particular field. In your view, you would do:

form_for @user do |f|
  f.label :owner
  f.check_box :owner
  f.submit
end

You would wrap this in the html code as needed.

Upvotes: 0

Chris G.
Chris G.

Reputation: 3981

<label>Owner: </label><input type="checkbox" name="owner" value="1" />

The owner key will be present in the post array if the checkbox is checked.

To make the checkbox checked when viewing the form after the form is submitted, you need to add checked="checked" as an attribute on the checkbox element.

Upvotes: 0

Related Questions