railstutorial.org,第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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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 ' 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! 

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

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

加:

 attr_accessor :password, :password_confirmation 

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

如果尚未执行此操作,只需将has_secure_password添加到用户模型文件中即可。

 class User < ActiveRecord::Base attr_accessible :email, :name, :password, :password_confirmation has_secure_password # .. end 

我刚遇到同样的问题。 我在Cloud9上这样做。

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

刷新我的文件树修复了这两个问题。

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

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

 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

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

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

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

 ActiveRecord::UnknownAttributeError: unknown attribute: password 

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

不幸的是,教程会立即告诉你注释掉has_secure_password所以使用潜水的答案作为替身可能不是一个坏主意,直到教程你取消注释。

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

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

我在这篇文章中写到了这个:

Rails – 未知的属性密码

如果我错了,请纠正我。