2015-04-03 86 views
1

因此,我知道有大約50萬個問題繞過編輯用戶的設計密碼要求,如果他們通過Facebook進行身份驗證。我保證我讀了至少75%,但仍然無法弄清楚。設計編輯用戶不保存

基本上,我已經按照Carl Edward & Laurie Laine's SO answer here爲Devise創建了一個註冊控制器,如果用戶正在編輯他們的帳戶並且用戶從Facebook登錄,那麼這將允許我繞過密碼驗證。用下面的代碼,它終於不會拋出錯誤,但我的更新屬性都沒有保存。

class RegistrationsController < Devise::RegistrationsController 

    def update_resource(resource, params) 
    if current_user.provider == "facebook" 
     params.delete("current_password") 
     resource.update_without_password(params) 
    else 
     resource.update_with_password(params) 
    end 
    end 

    def update 
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update) 


    # required for settings form to submit when password is left blank 
    if account_update_params[:password].blank? 
     account_update_params.delete("password") 
     account_update_params.delete("password_confirmation") 
    end 

    @user = User.find(current_user.id) 
    if @user.update_attributes(account_update_params) 
     @user.update(account_update_params) 
     set_flash_message :notice, :updated 
     update_resource(@user,account_update_params) 
     # Sign in the user bypassing validation in case their password changed 
     sign_in @user, :bypass => true 
     redirect_to after_update_path_for(@user) 
    else 
     render "edit" 
    end 
    end 
end 

我簡直想不出什麼我做錯了,但每次我嘗試更新我的用戶配置文件作爲登錄的用戶被Facebook認證,沒事就我的個人資料更改或當我查詢數據庫我安慰。

+0

你爲什麼叫@ user.update(account_update_params)後@ user.update_attributes(account_update_params)? update_attributes()只是update()的別名。 – 2015-04-03 04:27:13

+0

我補充說,因爲它不會保存,我認爲這可能會使它開始保存更新的屬性。我會把它拿出來。謝謝!任何想法爲什麼它沒有保存? – ChiefRockaChris 2015-04-03 04:46:05

+0

保存用戶時在日誌中看到什麼? – 2015-04-03 05:07:27

回答

1

事實證明,即使必要的屬性出現在我的設計參數殺菌器中,我錯誤地將:account_update參數命名爲:update。

之前(不工作):

def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit({ roles: [] }, :name,:email, :password, :password_confirmation) } 
     devise_parameter_sanitizer.for(:update) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation, :avatar,:current_password, :about,:user, :name) } 
    end 
end 

後(工作):

def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit({ roles: [] }, :name,:email, :password, :password_confirmation) } 
     devise_parameter_sanitizer.for(:account_update) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation, :avatar,:current_password, :about,:user, :name) } 
    end 
end