所有人。我遇到了以下问题:在实现has_password之后?在7.2.3节中,RSpec显示了以下错误,例如“应该创建一个给定有效属性的新实例”测试
(
1)用户应该创建一个新实例,给出有效的属性: User.create!(@attr) ArgumentError:参数的错误数量(1表示0) #./app/model/user.rb:42: in
secure_hash' # ./app/models/user.rb:39:inmake_salt‘#./app/model/user.rb:30:inencrypt_password' # ./spec/models/user_spec.rb:14:inblock (2个级别)
完成在1.47秒1例,1失败<-从(1)运行完成!
我不明白到底是什么导致了这个问题。以下是我的user.rb代码:
require 'digest'
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
email_regex = /\A[\w+\-.]+@[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它能是什么?提前谢谢你!
发布于 2011-03-06 15:30:47
您的secure_hash方法需要使用一个参数。
def secure_hash(string)
Digest::SHA2.hexdigest(string)
endhttps://stackoverflow.com/questions/5211210
复制相似问题