2011-06-17 78 views
2

我想2個複選框添加到編輯用戶登記表,但是當我提交表單的屬性沒有得到更新。它說: 「」 twitter_share 「=>」 1" , 「facebook_share」=> 「1」,」但是,這並不在數據庫中註冊。的Rails 3:如何使一個複選框工作形式

我在做什麼錯?

形式:

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

用戶模式:

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

登錄提交:

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 

回答

0

注意twitter_sharefacebook_share參數怎麼也user參數之外:

參數: 「?」{ 「UTF-8」=> ... 「twitter_share」=> 「1」, 「facebook_share」=> 「1」, 「用戶」=> { 「電子郵件」 ...... ..

他們需要的user裏面的參數。

所以,用f.check_box取代f.check_box_tag這才能正常工作。

例子:<%= f.check_box :twitter_share %>

+0

真棒感謝,但現在我得到一個新的錯誤http://www.pastie.org/2084453任何想法? –

+0

刪除括號? – Zabba

+0

即使沒有括號,我也會得到同樣的錯誤,http://www.pastie.org/2084500 –

相關問題