简体   繁体   English

设计没有密码的编辑用户

[英]Devise edit user without password

I was reading the documentation on devise on how to allow users to edit their account without password. 我正在阅读有关如何允许用户无需密码即可编辑其帐户的devise文档 Follow their first suggestion, I get an 遵循他们的第一个建议,我得到了

ActiveRecord::RecordInvalid Exception: Validation failed: Password can't be blank, Password confirmation can't be blank

my controller: 我的控制器:

class UserPreferencesController < Devise::RegistrationsController

  def update_profile
    @user = User.find(current_user.id)
    if @user.update!(account_params)
      sign_in @user, :bypass
      render json: [], status: 201
    else
      render json: [], status: 422
    end
  end

  def account_params
    params[:user].permit(:first_name, :last_name, :email, :sex, :phone_number, :location, :birthday)
  end

end

I tried the second 我尝试了第二

def update_resource(resource, params)
  resource.update_without_password(params)
end

Same error. 同样的错误。 So how do I update the user in devise without password and via json 那么我该如何在没有密码的情况下通过json更新用户

Override the password_required? 覆盖password_required? in the model you are updating: 在您要更新的模型中:

def password_required?
  false
end

That would be enough to edit/update the resource without providing the password. 无需提供密码就足以编辑/更新资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM