如何在密码更改时覆盖设计错误消息
如何自定义错误消息被覆盖的divise密码控制器?
class PasswordsController 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在此方法中可用,但它包含默认消息,例如Email not found
Email can't be blank
并且Email can't be blank
。 我需要自定义此消息。 我试图删除:validatable
从我的用户模型中:validatable
并添加自定义validation器,但这仅适用于从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,他为我回答了几乎相同的问题 。
设计消息位于config / locales / devise.en.yml中
我不确定你试图覆盖哪条消息,但这就是你想要做的。
这不是理想的,但基于这个相关的票据,我已经得到它与以下(我知道有点黑客,但它的工作):
module DeviseHelper def devise_error_messages! resource.errors.full_messages.map { |msg| msg == 'Email not found' ? 'The email address you entered could not be found. Please try again with other information.' : msg }.join('
') end end
将它放在/app/helpers
目录中名为devise_helper.rb
的模块中
将其添加到routes.rb中
devise_for :users, controllers: { passwords: 'passwords' }
要么
devise_for :users, :controllers => { :passwords => 'passwords' }