如何在密码更改时覆盖设计错误消息

rom*_*man 14 ruby ruby-on-rails devise ruby-on-rails-3

如何自定义错误消息被覆盖的divise密码控制器?

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
Run Code Online (Sandbox Code Playgroud)

resource.errors在此方法中可用,但它包含默认消息,例如Email not foundEmail can't be blank.我需要自定义此消息.我试图:validatable从我的用户模型中删除并添加自定义验证器,但这仅适用于从Devise :: RegistrationsController派生的自定义注册控制器,而不适用于自定义密码控制器.

有什么解决方案吗?

Jus*_*tin 15

答案是修改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)"
Run Code Online (Sandbox Code Playgroud)

归功于Vimsha,他为我回答了几乎相同的问题.


Jes*_*ott 7

设计消息位于config/locales/devise.en.yml中

我不确定你试图覆盖哪条消息,但这就是你想要做的.

  • 我想要覆盖的消息不在那里.我想要更改的这些消息是验证失败时使用的默认消息,`config/locales/devise.en.yml`包含设计信息消息. (3认同)