[英]ArgumentError in Chapter 7 of “Rails Tutorial” by Michael Hartl
大家。 我有以下问题:实现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:in
user.rb:secure_hash' # ./app/models/user.rb:39:in
make_salt'#。/ app /encrypt_password' # ./spec/models/user_spec.rb:14:in
secure_hash' # ./app/models/user.rb:39:in
30:secure_hash' # ./app/models/user.rb:39:in
密码'#。/ spec /encrypt_password' # ./spec/models/user_spec.rb:14:in
user_spec.rb:encrypt_password' # ./spec/models/user_spec.rb:14:in
block(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
它能是什么? 先感谢您!
你的secure_hash方法需要参数。
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.