2011-04-18 4 views
1

이것은 아침 내 엉덩이를 걷어 찼다.Michael Hartl 's Rails 3 튜토리얼 : has_password 문제? 방법 제 7 장

지금 당장 Michael Hartl의 우수 Ruby on Rails 3 튜토리얼의 7.2.4 장을 시작하고 있는데 몇 가지 문제가 있습니다. 이 섹션에서는 레일 콘솔 샌드 박스에서 has_password? 메소드를 빠르게 확인하는 것으로 시작합니다.

ruby-1.9.2-p180 :001 > User 
=> User(id: integer, name: string, email: string, created_at: datetime, updated 
updated_at: datetime, encrypted_password: string, salt: string) 
ruby-1.9.2-p180 :002 > User.create(:name => "John Pavlick", :email => "jmpavlick 
@gmail.com", :password => "foobar", :password_confirmation => "foobar") 
=> #<User id: nil, name: "John Pavlick", email: "[email protected]", created_ 
at: nil, updated_at: nil, encrypted_password: nil, salt: nil> 
ruby-1.9.2-p180 :003 > user = User.find_by_email("[email protected]" 
ruby-1.9.2-p180 :004?> ) 
=> #<User id: 1, name: "John Pavlick", email: "[email protected]", created_at 
: "2011-04-15 15:11:46", updated_at: "2011-04-15 15:11:46", encrypted_password: 
nil, salt: nil> 
ruby-1.9.2-p180 :005 > user.has_password?("foobar") 
=> false 

true을 반환해야한다 : 여기에 입력 한 것입니다.

여기 내 user.rb 모델의 :

RSpec 테스트
class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :name, :email, :password, :password_confirmation 

    email_regex = /^[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+$/i 

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

    # Automatically create the virtual attribute 'password confirmation' 
    validates :password, :presence => true, 
              :confirmation => true, 
              :length => { :within => 6..40 } 

    before_save :encrypt_password 

    # Return true if the user's password matches the submitted password 

    def has_password?(submitted_password) 
     encrypted_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 

모두 완벽하게 통과, 그리고 지금까지 내가 말할 수있는, 내가 책에있는 그대로 모든 것을 코딩 한 - 나는 심지어 복사/붙여 넣은 일부 코드는 확인합니다. 나는 이것이 실패 할 이유가 전혀 없기 때문에 어떤 도움이 생명의 은인이 될 것입니다. 여기

이 책에 대한 링크의 온라인 : 당신의 생성 호출 후 [link]

답변

4

하는 ID 또는 암호화 된 암호가없는 반환 된 개체가 ... 나는 유효성 검사가 실패 것으로 의심 통지 및 기록이다 이미 레코드가 있기 때문에 생성되지 않았습니까?

+0

정말 고마워요! 그 정보를 가지고 데이터베이스에 레코드를 만들었습니다.': name'과': email :'에 다른 정보를 사용했을 때, 그것은 완벽하게 작동했습니다! 다시 한 번 감사드립니다! – eckza

+0

DGM이 말한 것에 덧붙여'rake db : reset'을 실행 한 다음 레코드 생성을 시도 할 수 있어야합니다. (나는 생각한다.) – Tass

관련 문제