6

我已经完成了 railstutorial 的第 6 章,但是在我添加&后,我的所有用户模型规范都开始失败,并出现以下错误:passwordpassword_confirmation

Failures:

  1) User 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  2) User 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  3) User 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  4) User 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  5) User 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  6) User 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  7) User 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  8) User when name is not present 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  9) User when email is not present 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  10) User when name is too long 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  11) User when email format is invalid should be invalid
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  12) User when email format is valid should be valid
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  13) User when email address is already taken 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  14) User when password is not present 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  15) User when password confirmation is nil 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  16) User when password doesn't match confirmation 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  17) User return value of authenticate method with valid password 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  18) User return value of authenticate method with invalid password 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  19) User return value of authenticate method with invalid password 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

  20) User with a password that's too short 
     Failure/Error: @user = User.new(name: "Example User", email: "user@example.com",
     ActiveRecord::UnknownAttributeError:
       unknown attribute: password
     # ./spec/models/user_spec.rb:17:in `new'
     # ./spec/models/user_spec.rb:17:in `block (2 levels) in <top (required)>'

Finished in 7.43 seconds
39 examples, 20 failures, 5 pending

Failed examples:

rspec ./spec/models/user_spec.rb:22 # User 
rspec ./spec/models/user_spec.rb:23 # User 
rspec ./spec/models/user_spec.rb:24 # User 
rspec ./spec/models/user_spec.rb:25 # User 
rspec ./spec/models/user_spec.rb:26 # User 
rspec ./spec/models/user_spec.rb:27 # User 
rspec ./spec/models/user_spec.rb:28 # User 
rspec ./spec/models/user_spec.rb:32 # User when name is not present 
rspec ./spec/models/user_spec.rb:37 # User when email is not present 
rspec ./spec/models/user_spec.rb:42 # User when name is too long 
rspec ./spec/models/user_spec.rb:46 # User when email format is invalid should be invalid
rspec ./spec/models/user_spec.rb:57 # User when email format is valid should be valid
rspec ./spec/models/user_spec.rb:73 # User when email address is already taken 
rspec ./spec/models/user_spec.rb:78 # User when password is not present 
rspec ./spec/models/user_spec.rb:83 # User when password confirmation is nil 
rspec ./spec/models/user_spec.rb:88 # User when password doesn't match confirmation 
rspec ./spec/models/user_spec.rb:96 # User return value of authenticate method with valid password 
rspec ./spec/models/user_spec.rb:102 # User return value of authenticate method with invalid password 
rspec ./spec/models/user_spec.rb:103 # User return value of authenticate method with invalid password 
rspec ./spec/models/user_spec.rb:109 # User with a password that's too short 
rake aborted!

用户模型可以在这里找到现在,我已经将源代码与原始模型进行了比较,但似乎我遗漏了一些东西。不要它是什么

谁能告诉我为什么会出现这个错误?

4

6 回答 6

17

has_secure_password如果您还没有这样做, 只需添加到用户模型文件中。

class User < ActiveRecord::Base
 attr_accessible :email, :name, :password, :password_confirmation
 has_secure_password
 # ..
end
于 2012-09-10T18:43:45.937 回答
8

添加:

attr_accessor :password, :password_confirmation

然后,本教程将使您将摘要保存在password_digest

于 2012-08-27T12:55:09.923 回答
2

我只是有同样的问题。我在 Cloud9 上做这个。

我注意到我的测试也没有全部运行。

刷新我的文件树解决了这两个问题。

我不需要将 :password 或 :password_confirmation 直接放在模型中(或者根本不需要从教程中进行任何编码更改。

于 2015-06-07T17:34:50.487 回答
1

我最终花了几个小时,找不到任何答案。我的问题最终出现在小写线上:

before_save { self.email = email.downcase }

他指定你可以用我做的不同方式来做这条线。见下文。

https://www.railstutorial.org/book/_single-page#sec-creating_and_authenticating_a_user

在代码清单 6.31 中,我们可以将赋值写成 self.email = self.email.downcase(其中 self 指代当前用户),但在 User 模型中,右侧的 self 关键字是可选的:self.email = email.downcase

不要这样做。它每次都给我错误消息,直到我恢复 before_save { self.email = email.downcase }

并删除: self.email = self.email.downcase

我是一个菜鸟,所以不完全确定为什么这会有所作为,但确实如此。

于 2016-01-22T20:37:50.707 回答
0

它正在产生这些错误,因为第一个before {}块试图添加 User 模型未指定的两个属性 -passwordpassword_confirmation.

因为这个before {}块在每次测试之前运行(这就是重点),所以每次测试都会出错并且它们都失败了。这并不是说测试本身失败了,而是ActiveRecord不知道如何处理这些属性,并且在每个测试真正运行之前都会产生错误:

ActiveRecord::UnknownAttributeError:
unknown attribute: password

正如codeneko的回答所说,只需在教程中前进并放入has_secure_password用户模型文件即可解决此问题。这告诉ActiveRecord接受passwordpassword confirmation属性,所有测试都将通过。

不幸的是,教程会立即告诉您注释掉,has_secure_password因此在教程让您取消注释之前,使用潜水的答案作为替代可能不是一个坏主意。

于 2014-07-21T18:12:31.890 回答
0

我认为 attr_accessor :password, :password_confirmation为用户模型添加的公认答案是错误的。因为如果has_secure_password虚拟地创建这些属性,这意味着它出于某种原因(安全性)这样做。因此,解决方案可能是在您创建用户测试对象的任何地方调用 BCrypt:

require 'bcrypt'

    @user = User.new(name: 'test', password: BCrypt::Password.create("my password"), password_confirmation: 'my password')

我在这篇文章中写道:

Rails - 未知属性密码

如果我错了,请纠正我。

于 2017-04-09T13:50:58.750 回答