如何覆盖有关密码更改的设计错误消息 [英] How to override devise error messages on password change

查看:125
本文介绍了如何覆盖有关密码更改的设计错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I customize error messages the overridden divise passwords controller?

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

resource.errors is available in this method but it contains default messages such as Email not found and Email can't be blank. i need to customize this messages. I've tried to remove :validatable from my user model and add custom validators but this works only for my custom registrations controller derived from Devise::RegistrationsController and not for custom passwords controller.

有没有解决方案?

推荐答案

答案是修改 config / locales / devise.en.yml ,但是您必须添加设置,默认情况下不会。

The answer is to modify config/locales/devise.en.yml but you must add the settings, they are not there by default.

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            password:
              confirmation: "does not match"
              too_short: "is too short (minimum is %{count} characters)"

对于Vimsha来说,信誉是几乎相同的问题为我。

Credit for this goes to Vimsha who answered virtually the same question for me.

这篇关于如何覆盖有关密码更改的设计错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆