设计:如何在密码更改时覆盖设计错误消息 [英] Devise: How to override devise error messages on password change

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

问题描述

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

How can I customize error messages to override devise 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天全站免登陆