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操作中使用的任何内容,以便从表单中设置新参数.
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)
我解决了这个问题如下:如果表单提交密码,则需要填写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)
如果您仍然希望支持密码更改但使其成为可选,请检查以下内容的可用性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 存在,您可以继续并更新密码,否则忽略它并在没有密码的情况下更新。
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/
| 归档时间: |
|
| 查看次数: |
46990 次 |
| 最近记录: |