Ruby on rails Rails教程ch 7.2.3是否有密码?未定义方法
在Michael Hartl的Ruby on Rails教程中,rspec is返回以下错误:Ruby on rails Rails教程ch 7.2.3是否有密码?未定义方法,ruby-on-rails,Ruby On Rails,在Michael Hartl的Ruby on Rails教程中,rspec is返回以下错误: Failures: 1) User has_password? method should be true if the passwords match Failure/Error: @user.has_password?(@attr[:password].should be_true) NoMethodError: undefined method `has_password?' for
Failures:
1) User has_password? method should be true if the passwords match
Failure/Error: @user.has_password?(@attr[:password].should be_true)
NoMethodError:
undefined method `has_password?' for nil:NilClass
# ./spec/models/user_spec.rb:132:in `block (3 levels) in <top (required)>'
2) User has_password? method should be false if the passwords don't match
Failure/Error: @user.has_password?("invalid").should be_false
NoMethodError:
undefined method `has_password?' for nil:NilClass
# ./spec/models/user_spec.rb:136:in `block (3 levels) in <top (required)>'
Finished in 0.23931 seconds
18 examples, 2 failures
Failed examples:
rspec ./spec/models/user_spec.rb:131 # User has_password? method should be true if the passwords match
rspec ./spec/models/user_spec.rb:135 # User has_password? method should be false if the passwords don't match
故障:
1) 用户有密码吗?如果密码匹配,则方法应为true
失败/错误:@user.has_password?(@attr[:password]。应为_true)
命名错误:
nil:NilClass的未定义方法“has_password?”
#./spec/models/user_spec.rb:132:in'block(3层)in'
2) 用户有密码吗?如果密码不匹配,则方法应为false
失败/错误:@user.has\u密码?(“无效”)。应为\u false
命名错误:
nil:NilClass的未定义方法“has_password?”
#./spec/models/user_spec.rb:136:in'block(3层)in'
以0.23931秒完成
18例,2次失败
失败的示例:
rspec./spec/models/user_spec.rb:131#用户有密码吗?如果密码匹配,则方法应为true
rspec./spec/models/user_spec.rb:135#用户有密码吗?如果密码不匹配,则方法应为false
在控制台中,我还得到一个未定义的局部变量错误“密码确认”
我已经彻底检查了我的代码,没有发现差异,但我显然做错了
以下是我的用户模型:
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
def has_password?(submitted_password)
self.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
需要“摘要”
类用户true,
:length=>{:max=>50}
验证:email,:presence=>true,
:format=>{:with=>email_regex},
:唯一性=>{:区分大小写=>false}
#自动创建虚拟属性“密码确认”。
验证:password,:presence=>true,
:confirmation=>true,
:length=>{:within=>6..40}
保存前:加密密码
def有密码?(已提交密码)
self.encrypted\u password==加密(提交的\u密码)
结束
私有的
def加密密码
self.salt=如果有新记录,则制作盐?
self.encrypted_password=加密(密码)
结束
def加密(字符串)
安全散列(“#{salt}--#{string}”)
结束
def制盐
安全散列(“#{Time.now.utc}--#{password}”)
结束
def secure_散列(字符串)
摘要::SHA2.hexdigest(字符串)
结束
结束
确保这一位在您的规范中,听起来好像不见了
before(:each) do
@user = User.create!(@attr)
end
我们需要看看说明书;问题是你调用它的是nil,而不是这个方法不存在。您确定正在加载和检索适当的测试数据吗?如何在规范中设置@user?听起来你还没有初始化它。是的,has\u密码中没有这个?方法。谢谢