railstutorial.org,第6章。未知属性:密码

时间:2012-08-27 12:52:11

标签: ruby-on-rails ruby-on-rails-3 rspec tdd railstutorial.org

我已完成Chapter 6 of railstutorial,但在我添加password&之后,我的所有User model specs都已开始失败password_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!

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

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

6 个答案:

答案 0 :(得分:16)

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

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

答案 1 :(得分:7)

添加:

attr_accessor :password, :password_confirmation

然后,本教程将使您在password_digest

中保存摘要

答案 2 :(得分:1)

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

我注意到我的测试都没有运行。

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

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

答案 3 :(得分: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

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

答案 4 :(得分:0)

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

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

ActiveRecord::UnknownAttributeError:
unknown attribute: password

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

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

答案 5 :(得分:0)

我认为接受的答案,补充一下 用户模型的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 - Unknown attribute password

如果我错了,请纠正我。