has_password有问题吗? railstutorial.org中的方法(第7章)

时间:2011-07-28 23:22:30

标签: ruby-on-rails railstutorial.org

我在this question中描述了完全相同的问题,但显然原因不同。

This是本书的相关部分。

据我所知,我完全按照本书的说法完成了这项工作,我的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 >

如您所见,没有任何记录已经创建,就像我链接的其他问题一样。

1 个答案:

答案 0 :(得分:1)

您似乎意外混淆了encrypt_passwordencrypted_password

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

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

问题在于您使用了encrypt_password,因此如果您查找记录,则普通密码属性password为零,因此您的encrypt_password方法会self.encrypted_password = encrypt(nil) < / p>