1
有必要使Devise中的授權錯誤與RubyOnRails中的ActiveModel :: Errors一樣工作,即錯誤在哈希中由與字段名相對應的鍵可用。如何在Devise自定義身份驗證策略中添加自定義錯誤密鑰?
我想從服務器得到這樣的響應{ 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
感謝您的回答。但你不太明白我的問題。問題不在於錯誤的翻譯,而在於「Devise :: Strategies :: DatabaseAuthenticatable#authenticate!」方法的答案格式。 –