Ruby on rails 如何在设计自定义身份验证策略时添加自定义错误密钥?

Ruby on rails 如何在设计自定义身份验证策略时添加自定义错误密钥?,ruby-on-rails,authentication,devise,Ruby On Rails,Authentication,Devise,有必要使Desive中的授权错误以与RubyOnRails中的ActiveModel::errors相同的方式工作,也就是说,该错误通过字段名对应的键在哈希中可用 我希望像这样从服务器获取响应{errors:{user:{user not found'}}或{errors:{password:{Invalid password'}}},但我仍然从服务器获取{error:{user not found'} 我的自定义策略: module Devise module Strategies

有必要使Desive中的授权错误以与RubyOnRails中的ActiveModel::errors相同的方式工作,也就是说,该错误通过字段名对应的键在哈希中可用

我希望像这样从服务器获取响应
{errors:{user:{user not found'}}
{errors:{password:{Invalid password'}}}
,但我仍然从服务器获取
{error:{user not found'}

我的自定义策略:

module Devise
  module Strategies
    class Password < Authenticatable

      def authenticate!
        resource  = password.present? && mapping.to.find_for_database_authentication(authentication_hash)

        if resource
          if validate(resource){ resource.valid_password?(password) }
            remember_me(resource)
            resource.after_database_authentication
            success!(resource)
          else
            errors.add(:password, I18n.t('devise.failure.invalid_password'))
            fail!(:invalid_password)
          end
        else
          errors.add(:user, I18n.t('devise.failure.user_not_found'))
          fail!(:user_not_found)
        end
      end

    end
  end
end
模块设计
模块策略
类密码
答案是修改config/locales/designe.en.yml,但您必须添加设置,它们默认不在那里。

答案是修改config/locales/designe.en.yml,但您必须添加设置,它们默认不在那里。

谢谢您的回答。但是你不太明白我的问题。问题不在于错误的翻译,而在于方法
designe::Strategies::databaseauthenticable#authenticate谢谢你的回答。但是你不太明白我的问题。问题不在于错误的翻译,而在于方法
designe::Strategies::databaseauthenticable#authenticate