2012-01-17 44 views
0

同時使用下列命令更新從鐵軌控制檯數據庫:我得到一個零對象引用的時候,我不希望它-ROR

user=User.first 
    User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1 
=> nil 

,然後當我使用更新

user.update_attributes(:email => "[email protected]", :password => "foobar", :password_confirmation => "foobar")` 

我得到此錯誤:

NoMethodError: You have a nil object when you didn't expect it! 
You might have expected an instance of ActiveRecord::Base. 
The error occurred while evaluating nil.update_attributes 

我想從mhartl教程中學習ROR,這與本教程有關。我發現在網站上已經回答了類似的問題,但無法做出任何表示。所以張貼我的具體錯誤在這裏。

+2

因此,您顯然在數據庫中沒有用戶。 – 2012-01-17 07:04:42

回答

4

User.first返回nil所以你不會得到一個對象,因爲數據庫中沒有對象!並且nil沒有update_attributes方法,因爲這是由ActiveRecord::Base繼承的。你的問題是你的數據庫中沒有數據!

+0

Woah減速:)小心那些錯別字。 – 2012-01-17 07:24:39

+0

其正確...似乎用戶表中沒有記錄。 – 2012-01-18 07:39:23

+0

這是一個愚蠢的錯誤。謝謝回覆。 – 2012-01-19 11:41:37

相關問題