更新设备帐户时,当前密码不能为空

 花懋1274238844 发布于 2023-02-09 14:47

我正在使用devise,我想允许用户更新他的帐户(电子邮件和密码).因此,当我点击时edit_user_registration_path,我会看到一个用户可以更改其电子邮件和密码的页面.但是在提交此update表单时,我不断收到此消息:

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

有人可以解释一下吗?

2 个回答
  • 默认情况下,Devise有三个密码字段edit_user_registration:password,password_confirmation和current_password:默认注册/ edit.html.erb

    任何更改都需要current_password; 如果不应该更改密码,则可以将其他两个留空.

    2023-02-09 14:49 回答
  • 将此代码放在您的用户模型中:

    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
    

    2023-02-09 14:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有