ruby-on-rails – 如何在密码更改时覆盖设计错误消息

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何在密码更改时覆盖设计错误消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何自定义错误消息被覆盖的密码控制器?
  1. class PasswordsController < Devise::PasswordsController
  2. def create
  3. self.resource = resource_class.send_reset_password_instructions(params[resource_name])
  4.  
  5. if resource.errors.empty?
  6. set_flash_message(:notice,:send_instructions) if is_navigational_format?
  7. respond_with resource,:location => home_path
  8. else
  9. binding.pry
  10. flash[:devise_password_error] = (resource.errors.map do |key,value|
  11. value.capitalize
  12. end).flatten.join('|')
  13. redirect_to home_path and return
  14. end
  15. end
  16. def edit
  17. self.resource = resource_class.new
  18. resource.reset_password_token = params[:reset_password_token]
  19. end
  20. end

resource.errors可用于此方法,但它包含默认消息,如电子邮件未找到,电子邮件不能为空.我需要定制这个消息.我试图从我的用户模型中删除:可验证,并添加自定义验证器,但这仅适用于我从Devise :: RegistrationsController派生的自定义注册控制器,而不适用于自定义密码控制器.

有什么解决方案吗?

解决方法

答案是修改config / locales / devise.en.yml,但是您必须添加设置,默认情况下不存在.
  1. en:
  2. activerecord:
  3. errors:
  4. models:
  5. user:
  6. attributes:
  7. password:
  8. confirmation: "does not match"
  9. too_short: "is too short (minimum is %{count} characters)"

对于这样的信用给了Vimsha,对我来说几乎相同的question.

猜你在找的Ruby相关文章