更新设计帐号时当前密码不能为空 [英] Current password can't be blank when updating devise account
本文介绍了更新设计帐号时当前密码不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 devise
,并且我希望允许用户更新其帐户(电子邮件和密码).因此,当我单击 edit_user_registration_path
时,我会看到一个页面,用户可以在其中更改他的电子邮件和密码.但是在提交此 update
表单时,我不断收到以下消息:
I am using devise
and I want to allow the user to update his account (email & password). So when I click on edit_user_registration_path
, I get a page where the user can change his email and password. But when submitting this update
form I constantly get this message :
1 error prohibited this user from being saved: ×
Current password can't be blank
在我的 ApplicationController
中,我有
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :surname, :email, :user_name, :terms_of_service, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation) }
end
有人可以解释吗?
推荐答案
将此代码放入您的用户模型:
Place this code in your User model:
def update_with_password(params, *options)
current_password = params.delete(:current_password)
if params[:password].blank?
params.delete(:password)
params.delete(:password_confirmation) if params[:password_confirmation].blank?
end
result = if params[:password].blank? || valid_password?(current_password)
update_attributes(params, *options)
else
self.assign_attributes(params, *options)
self.valid?
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
false
end
clean_up_passwords
result
end
这篇关于更新设计帐号时当前密码不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文