2011-03-06 76 views
0

everyone。我遇到以下問題: 執行has_password後?在第7.2.3 RSpec中顯示以下錯誤「應該創造給予有效屬性的新實例」試驗中,例如Michael Hartl的「Rails Tutorial」第7章中的參數錯誤

1) User should create a new instance given valid attributes Failure/Error: User.create!(@attr) ArgumentError: wrong number of arguments (1 for 0) # ./app/models/user.rb:42:in secure_hash' # ./app/models/user.rb:39:in make_salt' # ./app/models/user.rb:30:in encrypt_password' # ./spec/models/user_spec.rb:14:in block (2 levels) in '

Finished in 1.47 seconds 1 example, 1 failure <-- Slave(1) run done!

我不明白,究竟是什麼原因導致的問題。 這裏是我的user.rb代碼:

require 'digest' 

class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :name, :email, :password, :password_confirmation 

    email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\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 
    Digest::SHA2.hexdigest(string) 
    end 
end 

什麼可以嗎? 提前謝謝!

回答

2

您的secure_hash方法需要參數。

def secure_hash(string) 
    Digest::SHA2.hexdigest(string) 
end 
相關問題