2011-08-11 77 views
2

與軌道3 +設計,我希望用戶能夠更改他們的密碼:色器件,試圖更改密碼時警告:不能大規模指派保護的屬性:current_password

這裏是日誌輸出:

Started POST "/settings/password/update" for 127.0.0.1 at 2011-08-11 11:37:05 -0700 
    Processing by SettingsController#password_update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"QBZP/h0Xg1SOBWh0+JwzFRyC8X7pE50mx1CgeS6+iNY=", "user"=>{"current_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Change my password"} 
    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1 
    SQL (0.2ms) BEGIN 
WARNING: Can't mass-assign protected attributes: current_password 
    AREL (0.7ms) UPDATE "users" SET "encrypted_password" = '$2a$10$cjq9eWB41aqeukarMzyciO4adXB3g.gCSwP6OouV0HT9HZI3IVIa6', "updated_at" = '2011-08-11 18:37:06.326258' WHERE "users"."id" = 3 
[paperclip] Saving attachments. 
    SQL (25.7ms) COMMIT 
Redirected to http://localhost:3000/settings/account 
Completed 302 Found in 556ms 

這裏是password_update控制器方法:

def password_update 

    @user = current_user 

    if @user.update_attributes(params[:user]) 
     flash[:notice] = 'Password Updated' 
     redirect_to '/settings/account' 
    else 
     flash[:notice] = 'Error' 
     redirect_to '/settings/password' 
    end 

    end 

任何想法,爲什麼發生這種情況?我不是想設置current_password,但根據設計你需要通過它來更新密碼,對吧?謝謝

+0

奇怪的是它正在更新密碼,但後來出來後簽約用戶? – AnApprentice

回答

2

attr_accessible :current_password添加到您的用戶模型。

+3

當我嘗試這樣做時,會得到'unknown attribute:current_password'。 –

+1

我也是。 ':current_password'不是User對象的屬性。 – rodrigoalves

+6

然後,您可能需要將'attr_accessor:current_password'添加到您的模型中。這些鏈接也可能有所幫助:[1](https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password)和[2](https:// github.com/plataformatec/devise/issues/1620) – manafire

3

我寧願覆蓋update_without_password用戶模型的方法

def update_without_password(params={}) 
    params.delete(:current_password) 
    super(params) 
end 
相關問題