Dea*_*ear 5 ruby-on-rails devise
我试图'reset_password_token is invalid'完全覆盖Devise的错误消息.我想看看"password reset link has already been used."我怎么能这样做?没有看到这个字段或关键字devise.en.yml.
Reset password token is invalidmessage 是更新密码时引发的验证错误,而不是设备特定错误(其消息存储在devise.en.yml中)。
此验证发生在devise/passwords_controller#update方法中。代码如下:
# PUT /resource/password
def update
self.resource = resource_class.reset_password_by_token(resource_params)
yield resource if block_given?
if resource.errors.empty?
resource.unlock_access! if unlockable?(resource)
flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
set_flash_message(:notice, flash_message) if is_flashing_format?
sign_in(resource_name, resource)
respond_with resource, location: after_resetting_password_path_for(resource)
else
respond_with resource
end
end
Run Code Online (Sandbox Code Playgroud)
该self.resource = resource_class.reset_password_by_token(resource_params)行使用resource.errors与 Reset_password_token 无效相关的错误消息设置对象。
检查此行之后的值resource.errors将显示一个以结尾的大散列... @messages={:reset_password_token=>["is invalid"]}
该devise_error_messages方法将其重新格式化为“重置密码令牌无效”。
要更改此消息,应自定义密码控制器并将方法update更改为具有不同的错误消息哈希。
步骤如下:
1)自定义密码控制器的路由
# config/routes.rb
devise_for :users, :controllers => { :passwords => "passwords" }
Run Code Online (Sandbox Code Playgroud)
2)创建自定义密码控制器
# app/controllers/passwords_controller.rb
class PasswordsController < Devise::PasswordsController
end
Run Code Online (Sandbox Code Playgroud)
3)自定义更新方法更改错误信息:
# app/controllers/passwords_controller.rb
# Include the update method as it is fully, with the following changes in the else block:
def update
...
if resource.errors.empty?
...
else
if resource.errors.messages[:reset_password_token]
resource.errors.messages.delete(:reset_password_token)
resource.errors.messages[:password_reset_link] = ["has already been used"]
end
respond_with resource
end
Run Code Online (Sandbox Code Playgroud)
有关定制 Devise 控制器的更多信息
| 归档时间: |
|
| 查看次数: |
1144 次 |
| 最近记录: |