【发布时间】:2021-04-15 16:45:58
【问题描述】:
这是我更改密码的代码:
class RegistrationsController < Devise::RegistrationsController
protect_from_forgery
def update_password
if current_user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
sign_in(current_user, bypass: true)
redirect_to settings_path, notice: "updated"
else
redirect_to settings_path, alert: current_user.errors.full_messages
end
end
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
def after_sign_up_path_for(_resource)
end
def after_update_path_for(_resource)
settings_path
end
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name])
end
private
def user_params
# NOTE: Using `strong_parameters` gem
params.require(:user).permit(:current_password, :password, :password_confirmation)
end
end
如果我输入我的当前密码并设置一个新密码,则该代码有效,但当我输入正确的当前密码并且作为新密码+确认我留下空字段(空字符串)时,它不起作用。
密码不会被更改为“无密码”,但我会收到一条“已更新”的闪烁消息。如何防止这种情况?我能想到这个:
if current_user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
if params[:user][:password].blank?
redirect_to settings_path, alert: "blank pswd"
return
end
sign_in(current_user, bypass: true)
redirect_to settings_path, notice: "Your password has been updated!"
else
redirect_to settings_path, alert: current_user.errors.full_messages
end
但是,这个解决方案有点……丑陋。有没有更优雅的方式来处理这种情况?
提前谢谢你
【问题讨论】:
标签: ruby-on-rails ruby devise