railstutorial.org, Chapter 6. unknown attribute: p

2019-02-14 08:41发布

I have finished Chapter 6 of railstutorial but all my User model specs have started failing soon after I added password & password_confirmation with the following errors:

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!

The User Model can be found here Now, I have already compared the source to original but it seems I am missing something. Don't what it is

Can anyone tell me why this error is coming?

6条回答
可以哭但决不认输i
2楼-- · 2019-02-14 08:42

Add:

attr_accessor :password, :password_confirmation

The tutorial will then make you save a digest in password_digest

查看更多
仙女界的扛把子
3楼-- · 2019-02-14 08:47

I think the accepted answer, to add attr_accessor :password, :password_confirmation for User model is wrong. Because if has_secure_password creates those attributes virtually, that means it does it for some reason (security). So solution might be to invoke BCrypt wherever you create a User test object:

require 'bcrypt'

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

I wrote about this on this post:

Rails - Unknown attribute password

Please correct me if I am wrong.

查看更多
做自己的国王
4楼-- · 2019-02-14 08:58

I just had the same problem. I was doing this on Cloud9.

I noticed that my tests weren't all running either.

Refreshing my file tree fixed both these problems.

I didn't need to put :password or :password_confirmation directly in the model (or making any coding changes at all from the tutorial.

查看更多
Melony?
5楼-- · 2019-02-14 08:59

I ended up spending a few hours and couldn't find any answers. My problem ended up being on the downcase line:

before_save { self.email = email.downcase }

He specifies that you can do this line in a different way which I did. See below.

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

In Listing 6.31, we could have written the assignment as self.email = self.email.downcase (where self refers to the current user), but inside the User model the self keyword is optional on the right-hand side: self.email = email.downcase

Don't do it this way. It gave me error messages every time until I reverted to before_save { self.email = email.downcase }

and removed: self.email = self.email.downcase

I'm a noob so not completely sure why this made the difference but it did.

查看更多
疯言疯语
6楼-- · 2019-02-14 09:07

Just add has_secure_password into User model file if you haven't done this yet.

class User < ActiveRecord::Base
 attr_accessible :email, :name, :password, :password_confirmation
 has_secure_password
 # ..
end
查看更多
在下西门庆
7楼-- · 2019-02-14 09:08

It is producing these errors because the very first before {} block is trying to add two attributes that the User model doesn't specify - password and password_confirmation.

Because this before {} block is run before every test (that's the point), you are getting an error on every test and they're all failing. It's not that the test is failing, per se, but rather ActiveRecord doesn't know what to do with those attributes and is producing an error before each test is really even run:

ActiveRecord::UnknownAttributeError:
unknown attribute: password

As codeneko's answer says, this is fixed by simply moving forward in the tutorial and putting has_secure_password in the User model file. This tells ActiveRecord to accept password and password confirmation attributes and all the tests will pass.

Unfortunately the tutorial promptly tells you to comment out has_secure_password so using diving's answer as a stand-in is probably not a bad idea until the tutorial has you uncomment that.

查看更多
登录 后发表回答