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

2019-06-27 10:13发布

我已经完成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!

用户模型,可以发现这里现在,我已经比较源头上原来的,但似乎我失去了一些东西。 难道它不是什么

谁能告诉我,为什么这个错误会来吗?

Answer 1:

加:

attr_accessor :password, :password_confirmation

那么本教程将让你节省文摘password_digest



Answer 2:

只需添加has_secure_password为用户模型文件,如果你还没有这样做呢。

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


Answer 3:

我有同样的问题。 我是在CLOUD9这样做。

我注意到,我的测试并不都是处于运行。

刷新我的文件树固定这两个问题。

我并不需要把:密码或:password_confirmation直接在模型(或从教程进行任何更改代码在所有。



Answer 4:

我最终花了几个小时,但没有找到任何答案。 我的问题结束了对downcase行:

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.email = email.downcase

不要做这种方式。 它给了我的错误信息,每次直到我恢复到before_save { self.email = email.downcase }

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

我是一个小白所以不能完全知道为什么本作的差异,但它没有。



Answer 5:

因为第一个它是产生这些错误before {} -块尝试添加该用户模型不指定两个属性, passwordpassword_confirmation

因为这before {}块每次测试之前运行(这点),你得到的每一个测试错误,他们都失败了。 这是不是说这个测试失败本身,而是ActiveRecord不知道如何处理这些属性做,并产生错误的每个测试真的连运行之前:

ActiveRecord::UnknownAttributeError:
unknown attribute: password

作为codeneko的回答说,这是通过简单地在教程中前进,并把固定has_secure_password在用户模型文件。 这告诉ActiveRecord接受passwordpassword confirmation的属性和所有的测试将通过。

不幸的是,教程及时告诉你注释掉has_secure_password所以用潜水的回答作为一个独立的可能不是一个坏主意,直到教程有你取消注释。



Answer 6:

我想接受的答案,补充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的-未知属性的密码

如果我错了请纠正我。



文章来源: railstutorial.org, Chapter 6. unknown attribute: password