有许多问题(即:here和here)解决了这个问题(并且对其他人也有同样的问题),但他们都没有为我工作。编辑用户时出现“未经许可的参数: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_password
到user_params
。这样做会导致unknown attribute 'current_password' for User. error.(添加建议的attr_accessor
没有帮助。)
这是我第一次需要定制Devise。任何帮助表示赞赏!
当您在控制台中键入User.column_names时,您会得到什么? –
=> [ “ID”, “电子邮件”, “encrypted_password”, “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需要成为该列表的成员?我想我认为这是一种“伪”参数。 –
@JonathanDueck - 你绝对不需要在列表中包含'current_password'。 Devise专门为用户的帐户更新操作公开此参数。 – 31piy