2017-06-10 32 views
2

有許多問題(即:herehere)解決了這個問題(並且對其他人也有同樣的問題),但他們都沒有爲我工作。編輯用戶時出現「未經許可的參數:current_password」

使用Rails 5.0.0和Devise 4.2,最終我試圖讓管理員編輯其他用戶,並允許普通用戶編輯自己的帳戶。 This需要a few modifications,因爲默認情況下,Devise不會讓登錄用戶編輯其他用戶。

users/:id/edit視圖中的表格現在填充了正確的用戶,但更新失敗,日誌中有Unpermitted parameter: current_password。我相信我需要白名單:current_password,但沒有任何建議可以幫助我完成。

的routes.rb

devise_for :users, controllers: {registrations: 'registrations'}, path_prefix: 'my' 
resources :users 

(該path_prefix 「我」 的建議,以此來避免設計和:users資源之間的航線衝突。)

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController 
    before_action :configure_permitted_parameters, if: :devise_controller? 


    ... 

    protected 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) do |u| 
     u.permit(:password, :password_confirmation) 
    end 

    devise_parameter_sanitizer.for(:account_update) do |u| 
     u.permit(:password, :password_confirmation, :current_password) 
    end 
    end 
end 

使用rs_controller.rb

class UsersController < ApplicationController 
    before_action :set_user, only: [:edit, :show, :update, :destroy] 

    ... 

    def edit 
    end 


    def update 
    @user = User.find(params[:id]) 
    if @user.update(user_params) 
     redirect_to cohorts_path 
    else 
     render 'edit' 
    end 
    end 

    private 

    def set_user 
    @user = User.find(params[:id]) 
    end 

    def user_params 
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :roles, :nickname, :location, :headshot, :goals) 

    end 
end 

你可能會奇怪,爲什麼我沒有加:current_passworduser_params。這樣做會導致unknown attribute 'current_password' for User. error.(添加建議的attr_accessor沒有幫助。)

這是我第一次需要定製Devise。任何幫助表示讚賞!

+0

當您在控制檯中鍵入User.column_names時,您會得到什麼? –

+0

=> [ 「ID」, 「電子郵件」, 「encrypted_pa​​ssword」, 「reset_password_token」, 「reset_password_sent_at」, 「remember_created_at」, 「sign_in_count」, 「current_sign_in_at」, 「last_sign_in_at」, 「 current_sign_in_ip 「 」last_sign_in_ip「, 」created_at「, 」的updated_at「, 」角色「, 」FIRST_NAME「, 」姓氏「, 」current_cohort「, 」綽號「, 」位置「, 」目標「, 」他adshot「]是否current_password需要成爲該列表的成員?我想我認爲這是一種「僞」參數。 –

+0

@JonathanDueck - 你絕對不需要在列表中包含'current_password'。 Devise專門爲用戶的帳戶更新操作公開此參數。 – 31piy

回答

0

終於解決了這一點。我加the following我UsersController的更新操作:

if params[:user][:password].blank? && params[:user[:password_confirmation].blank? 
    params[:user].delete(:password) 
    params[:user].delete(:password_confirmation) 
end 

因爲它是失敗默默,the following也是有幫助的診斷更新操作:

Rails.logger.info(@your_object.errors.inspect) 

謝謝大家!高興有這個解決。

0

它添加在UsersController - >>> user_params功能 - >>>在許可證,我想你應該在允許添加它...

def user_params 
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :roles, :nickname, :location, :headshot, :goals, :current_password) <<<< THERE 

    end 
+0

我確認我在user.rb中有attr_accessor:current_password,並且:UsersController中的user_params中已允許current_password。我得到BEGIN和ROLLBACK,沒有錯誤信息來說明爲什麼更新沒有發生。 –

+0

你用這個命令生成了設計註冊控制器嗎?rails generate devise:controllers users –

+0

我寫了UsersController和RegistrationsController手動。 (我沒有意識到你可以用你建議的方式生成Devise控制器。) –