2012-08-27 88 views
5

我已經完成Chapter 6 of railstutorial,但我所有的User model specs已經開始,我添加password & password_confirmation與下面的錯誤後不久出現故障:railstutorial.org,第6章未知屬性:密碼

Failures: 

    1) User 
    Failure/Error: @user = User.new(name: "Example User", email: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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: "[email protected]", 
    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現在,我已經將源代碼與原始代碼進行了比較,但似乎我錯過了一些東西。不要它是什麼

有誰能告訴我爲什麼會出現這個錯誤?

回答

7

地址:

attr_accessor :password, :password_confirmation 

本教程將然後進行保存摘要中password_digest

+0

所以我需要添加'attr_accessible'&'attr_accessor'?但在[示例源代碼](https://github.com/railstutorial/sample_app_2nd_ed/blob/master/spec/models/user_spec.rb)中,只使用了'attr_accesible'? – CuriousMind

+2

是的,你需要兩個,每個服務於不同的目的。它在教程中缺失 – apneadiving

+0

很酷,感謝您的幫助:) – CuriousMind

16

只需添加has_secure_password爲用戶模型文件,如果你還沒有這樣做呢。 passwordpassword_confirmation -

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

這是更好的解決方案,因爲只是添加'attr_accessible'會生成另一個錯誤,因爲方法'authenticate'將是未定義的。這解決了這兩個錯誤。 – smessing

0

,因爲第一個before {}塊試圖添加兩個屬性的用戶模型不指定它是生產這些錯誤。

因爲這個before {}塊是在每次測試之前運行的(這就是要點),所以每次測試都會得到一個錯誤,它們都會失敗。這是不是說這個測試失敗本身,而是ActiveRecord不知道如何處理這些屬性做的,併產生錯誤之前,每個測試是真正甚至可以運行:

ActiveRecord::UnknownAttributeError: 
unknown attribute: password 

由於codeneko的回答說,這隻需在教程中向前移動並將has_secure_password放入用戶模型文件即可解決。這告訴ActiveRecord接受passwordpassword confirmation屬性,並且所有的測試都會通過。

不幸的是,本教程迅速告訴您註釋掉has_secure_password,因此使用潛水的答案作爲替代品可能不是一個壞主意,直到教程取消註釋爲止。

1

我剛剛有同樣的問題。 我在Cloud9上做了這個。

我注意到我的測試並未全部運行。

刷新我的文件樹修復了這兩個問題。

我並不需要把:密碼或:password_confirmation直接在模型(或從教程進行任何更改代碼在所有

1

我最終花了幾個小時,但沒有找到任何答案。 。我的問題結束了對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表示當前用戶),但在User模型中,self關鍵字在右側是可選的: self。 email = email.downcase

不要這樣做。它每次給我的錯誤消息,直到我恢復到 before_save { self.email = email.downcase }

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

我是小白所以不能完全知道爲什麼本作的差異,但它沒有。

0

我認爲接受的答案,爲用戶模型添加 attr_accessor :password, :password_confirmation是錯誤的。因爲如果has_secure_password虛擬創建這些屬性,這意味着它出於某種原因(安全性)。

require 'bcrypt' 

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

我寫這對這個職位:

Rails - Unknown attribute password

請糾正我,如果我錯了,那麼無論你創建一個用戶測試對象解決方案可能是調用BCrypt。