나는 정확히 같은 문제를 this question에 기술하고있다. 그러나 다른 이유는 명백하다.has_password에 문제가 있습니까? method of railstutorial.org (7 장)
This은 책의 관련 섹션입니다.
필자가 할 수있는 한 정확하게 책에서했던대로했으며, rspec 테스트는 모두 잘 작동합니다. 내가 말했듯이, 테스트가 잘 작동하지만, has_password된다
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+\-.][email protected][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
:
내 사용자 모델 코드는 무엇입니까? 콘솔에서 항상 false를 반환합니다. 이것은 내가 콘솔에서했던 것입니다 :
ruby-1.9.2-p180 :002 > User.create!(:name => 'userdude', :email => '[email protected]', :password => 'foobar', :password_confirmation => 'foobar')
=> #<User id: 2, name: "userdude", email: "[email protected]", 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: "[email protected]", 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: "[email protected]", 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: "[email protected]", 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 >
보시다시피, 내가 링크 된 다른 문제로, 이미 생성 된 기록이 없습니다.
예! 고맙습니다! 나는 미쳐 가고 있었다. 이제 나는 그것을 더 조심스럽게 읽었습니다. 나는 그것을 보았습니다 : encrypted_password를 잘못 입력했습니다. 아마 편집기의 자동 완성 일 것입니다. –
물론 테스트가 성공적으로 완료되었습니다 ... 나는 오류를 발견하지 못했을 것입니다. –