可以自定义password_digest以使用“password_digest”以外的列

时间:2012-11-16 18:03:34

标签: ruby-on-rails

我想用 ActiveModel :: SecurePassword 实现Rails身份验证,但我不想在用户模型中使用“password_digest”列。我如何告诉Rails使用“foo”列而不是“password_digest”?

BTW-我已经更正了这篇文章。我原来有“密码”而不是“password_digest”。抱歉!关于用户模型中的“password_digest”列,Ryan Bates在他的RailsCast 270-authentication-in-rails 中说“名称很重要,但你可以自定义” 。我想找出怎么样?谢谢!

1 个答案:

答案 0 :(得分:1)

根据源代码判断,它看起来不像你能......除非你想修补这种方法......

# File activemodel/lib/active_model/secure_password.rb, line 34
def has_secure_password
  # Load bcrypt-ruby only when has_secure_password is used.
  # This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
  gem 'bcrypt-ruby', '~> 3.0.0'
  require 'bcrypt'

  attr_reader :password

  validates_confirmation_of :password
  validates_presence_of     :password_digest

  include InstanceMethodsOnActivation

  if respond_to?(:attributes_protected_by_default)
    def self.attributes_protected_by_default
      super + ['password_digest']
    end
  end
end