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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何在密码更改时覆盖设计错误消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何自定义错误消息被覆盖的密码控制器?
class PasswordsController < Devise::PasswordsController
  def create
    self.resource = resource_class.send_reset_password_instructions(params[resource_name])

    if resource.errors.empty?
      set_flash_message(:notice,:send_instructions) if is_navigational_format?
      respond_with resource,:location => home_path
    else
      binding.pry
      flash[:devise_password_error] =  (resource.errors.map do |key,value|
        value.capitalize
      end).flatten.join('|')
      redirect_to home_path and return
    end
  end
  def edit
    self.resource = resource_class.new
    resource.reset_password_token = params[:reset_password_token]
  end
end

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

有什么解决方案吗?

解决方法

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

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

原文链接:https://www.f2er.com/ruby/273542.html

猜你在找的Ruby相关文章