Rails 3: How to make a checkbox work in a form

11,513

Note how the twitter_share and facebook_share parameters are outside of the user parameters:

Parameters: {"utf8"=>"?", ... "twitter_share"=>"1", "facebook_share"=>"1", "user"=>{"email" .....

They need to be inside the user parameters.

So, replace f.check_box_tag with f.check_box for this to work properly.

Example: <%= f.check_box :twitter_share %>

Share:
11,513

Related videos on Youtube

Author by

trying_hal9000

Updated on June 04, 2022

Comments

  • trying_hal9000 12 months

    I'm trying to add 2 checkboxes to the edit user registration form but the attributes don't get updates when I submit the form. It says ""twitter_share"=>"1", "facebook_share"=>"1"," but this doesn't register in the database.

    What am I doing wrong??

    Form:

    <h2>Edit <%= resource_name.to_s.humanize %></h2>
    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true, :html => { :method => :put }, :html => {:multipart => true}) do |f| %>  
      <%= devise_error_messages! %>
      <p><%= f.check_box_tag(:twitter_share) %>
      <%= f.label_tag(:twitter_share, "Share to Twitter") %></p>
      <p><%= f.check_box_tag(:facebook_share) %>
      <%= f.label_tag(:facebook_share, "Share to Facebook") %></p>
      <p><%= f.label :email %><br />
      <%= f.text_field :email %></p>
      <p><%= f.label :name %><br />
      <%= f.text_field :name %></p>
      <p><%= f.label :username %><br />
      <%= f.text_field :username %></p>
      <p><%= f.label :bio %><br />
      <%= f.text_field :bio %></p>
      <p><strong>In order to change password:</strong></p>
      <p><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
      <%= f.password_field :password %></p>
      <p><%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation %></p>
      <p><%= f.label :current_password %> <i></i><br />
      <%= f.password_field :current_password %></p>
      <p><strong>---------------------------</strong></p>
      <strong>Avatar</strong></br />
      <div class="avatar">
       <%= image_tag(@user.avatar_url(:avatartiny).to_s) if @user.avatar? %>
       <%= f.file_field :avatar %>
       <%= f.hidden_field :avatar_cache %><br />
      </div><br />
      <p><%= f.submit "Update" %></p>
    <% end %>
    

    User model:

      attr_accessible :email, :password, :password_confirmation, 
     :remember_me, :name, :avatar, :username, :bio, :twitter_share, :facebook_share
    

    Log of form submit:

    Started POST "/users" for 127.0.0.1 at 2011-06-17 13:00:48 -0700
      Processing by RegistrationsController#update as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"UVzD3cczDsHlBC9uRpDA+1kkIwV5OL9r6jlH4egbwVI=", "twitter_share"=>"1", "facebook_share"=>"1", "user"=>{"email"=>"[email protected]", "name"=>"Example user", "username"=>"Example", "bio"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]", "avatar_cache"=>""}, "commit"=>"Update"}
      User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
      CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
    WARNING: Can't mass-assign protected attributes: current_password, avatar_cache
      SQL (0.2ms)  SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('[email protected]')) AND ("users".id <> 2) LIMIT 1
      SQL (0.2ms)  SELECT COUNT(*) FROM "authentications" WHERE ("authentications".user_id = 2)
      SQL (0.1ms)  SELECT 1 FROM "users" WHERE ("users"."username" = 'Example') AND ("users".id <> 2) LIMIT 1
      CACHE (0.0ms)  SELECT 1 FROM "users" WHERE ("users"."username" = 'Example') AND ("users".id <> 2) LIMIT 1
      Slug Load (0.4ms)  SELECT "slugs".* FROM "slugs" WHERE ("slugs".sluggable_id = 2 AND "slugs".sluggable_type = 'User') ORDER BY id DESC LIMIT 1
      AREL (0.3ms)  UPDATE "users" SET "bio" = '', "encrypted_password" = '$2a$10$hu1TyBa.75o6rlfx9GF3herU15Jl87nKxmT9i3h6erCHFX20.2fze', "updated_at" = '2011-06-17 20:00:48.822900' WHERE "users"."id" = 2
    Redirected to http://localhost:3000/
    Completed 302 Found in 372ms
    

Related