设计没有密码的更新用户

kri*_*sna 73 ruby-on-rails devise

我想在没有密码的情况下更新用户属性.情况就像,如果密码和密码确认字段不是空白,那么我需要设计错误,如果它们是空白,则需要更新其他用户属性.我怎么能用设计做到这一点?

提前致谢!

Joh*_*ohn 87

我认为这是一个更好的解决方案:

if params[:user][:password].blank? && params[:user][:password_confirmation].blank?
    params[:user].delete(:password)
    params[:user].delete(:password_confirmation)
end
Run Code Online (Sandbox Code Playgroud)

这可以防止您只需从表单响应中删除密码字段即可更改Devise控制器(如果它是空白的).

请务必先使用此功能, @user.attributes = params[:user]或者在update操作中使用的任何内容,以便从表单中设置新参数.

  • @MoMolog遵循Devise教程的第一部分:https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password创建一个空白的自定义注册控制器.完成后,从此处复制标准Devise注册控制器上的更新功能:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb将John的代码放在顶部你的控制器.将"update_resource(resource,account_update_params)"替换为"@ user.update_attributes(account_update_params)" (5认同)
  • 你把它放在哪里? (4认同)
  • 该解决方案不考虑`current_password`.它适用于除了*字段之外还显示所有用户属性(名称,电子邮件等)以更改密码的情况.如果没有这样的逻辑,如果用户在保留密码字段为空的情况下提交更改了其他字段的表单,Devise将不允许更新任何属性. (4认同)

Dty*_*Dty 62

这是你在找什么?来自Devise wiki

允许用户在不提供密码的情况下编辑其帐户

它显示了如何为导轨3和导轨4进行操作

=======================

约翰的解决方案是一个更简单的选择


Dun*_*son 18

我认为使用Devise 3.2+你可以覆盖你的子类控制器中的update_resource.以下是最初提出的问题的示例:

class MyRegistrationsController < Devise::RegistrationsController
  protected

  def update_resource(resource, params)
    resource.update_without_password(params)
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 只需确保并更新您的路线:`devise_for:users,controllers:{registrations:'registrations'}` (3认同)
  • 截至2014年,这是最正确的答案. (2认同)

Mik*_*hko 7

我解决了这个问题如下:如果表单提交密码,则需要填写current_password字段或表单更新,不带密码和current_password

在模型中:

class User
  devise: bla-bla-bla

  attr_accessor :current_password
end
Run Code Online (Sandbox Code Playgroud)

在控制器中:

class Users::RegistrationsController <  Devise::RegistrationsController
  layout 'application'


   def update
    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)


    # custom logic
    if params[:user][:password].present?
      result = resource.update_with_password(params[resource_name])
    else
      result = resource.update_without_password(params[resource_name])
    end

    # standart devise behaviour
    if result
      if is_navigational_format?
        if resource.respond_to?(:pending_reconfirmation?) && resource.pending_reconfirmation?
          flash_key = :update_needs_confirmation
        end
        set_flash_message :notice, flash_key || :updated
      end
      sign_in resource_name, resource, :bypass => true
      respond_with resource, :location => after_update_path_for(resource)
    else
      clean_up_passwords resource
      respond_with resource
    end
  end
end
Run Code Online (Sandbox Code Playgroud)


Seb*_*ddd 5

如果您仍然希望支持密码更改但使其成为可选,请检查以下内容的可用性current_password

class MyRegistrationsController < Devise::RegistrationsController
  protected

  def update_resource(resource, params)
    if params[:current_password].blank?
     resource.update_without_password(params.except(:current_password))
    else
      resource.update_with_password(params)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这样,如果 current_password 存在,您可以继续并更新密码,否则忽略它并在没有密码的情况下更新。


Cha*_*hee 5

params[:user][:password] 在 rails 6 中不起作用

您必须将params[:user][:password]更改为params[:password]

删除所有参数中的 [:user]

我的源代码在下面

在运行此命令之前

rails generate devise:controllers users -c=registrations

class Users::RegistrationsController < Devise::RegistrationsController
  # before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  protected

  def update_resource(resource, params)
    puts "params ===> #{params}"
    if params[:password].blank? && params[:password_confirmation].blank?
      params.delete(:password)
      params.delete(:password_confirmation)
      params.delete(:current_password)
      resource.update_without_password(params)
    else
      super
    end
  end
end
Run Code Online (Sandbox Code Playgroud)


小智 5

对于 2020 年访问过的人来说,这是我发现的最简单的解决方案:

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  protected

  def update_resource(resource, params)
    # Require current password if user is trying to change password.
    return super if params["password"]&.present?

    # Allows user to update registration information without password.
    resource.update_without_password(params.except("current_password"))
  end
end
Run Code Online (Sandbox Code Playgroud)

在routes.rb中:

devise_for :users, controllers: {
  registrations: 'users/registrations'
}
Run Code Online (Sandbox Code Playgroud)

全部归功于:Masatoshi Nishiguchi

https://www.mnishiguchi.com/2017/11/24/rails-devise-edit-account-without-password/