0

我在这个问题中描述了完全相同的问题,但显然是出于不同的原因。

是本书的相关部分。

据我所知,我已经完全按照书中的内容完成了,并且我的 rspec 测试都运行良好。

我的用户模型代码是这样的:

require 'digest' #Needs digest to do password encryption


class User < ActiveRecord::Base

  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation
  validates :name, :presence => true,
            :length => { :maximum => 20 }

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, :presence => true,
                    :format => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }

  validates :password, :presence => true,
                       :confirmation =>true,
                       :length => { :within => 5..24 }

  before_save :encrypt_password

  def has_password?(submitted_password)
    encrypt_password == encrypt(submitted_password)
  end




  private

  def encrypt_password
     self.salt = make_salt if new_record?
     self.encrypted_password = encrypt(password)
   end

   def encrypt(string)
     secure_hash("#{salt}--#{string}")
   end

   def make_salt
     secure_hash("#{Time.now.utc}--#{password}")
   end

   def secure_hash(string)
     Digest::SHA2.hexdigest(string)
   end


end

正如我所说,测试工作正常,但是 has_password 呢?总是在控制台中返回 false。这就是我在控制台中所做的:

ruby-1.9.2-p180 :002 > User.create!(:name => 'userdude', :email => 'a@a.com', :password => 'foobar', :password_confirmation => 'foobar' )

=> #<User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88..."> 

ruby-1.9.2-p180 :003 > User.all
 => [#<User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88...">] 

ruby-1.9.2-p180 :004 > user = User.first
 => #<User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88..."> 

ruby-1.9.2-p180 :005 > user
 => #<User id: 2, name: "userdude", email: "a@a.com", created_at: "2011-07-28 23:13:40", updated_at: "2011-07-28 23:13:40", encrypted_password: "84f9a1348af1f7e6610fb1d0c7002efe96f43dc97d807b3ac00...", salt: "13cf7bfd386af4fcf282ae5fa4831eb9d1dff15e842be8cba88..."> 

ruby-1.9.2-p180 :006 > user.has_password?('foobar')
 => false 
ruby-1.9.2-p180 :007 >

如您所见,与我链接的另一个问题一样,还没有创建任何记录。

4

1 回答 1

1

好像你不小心混在一起encrypt_passwordencrypted_password

尝试将您的has_password?方法更改为:

  def has_password?(submitted_password)
    self.encrypted_password == encrypt(submitted_password)
  end

问题是您使用encrypt_password了 ,因此如果您查找记录,则普通密码属性password为 nil,因此您的encrypt_password方法可以self.encrypted_password = encrypt(nil)

于 2011-07-28T23:36:17.207 回答