0

通过以下命令从 Rails 控制台更新数据库时:

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

然后当我使用更新它时

user.update_attributes(:email => "example@railstutorial.org", :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,这与教程有关。我发现网站上已经回答了类似的问题,但无法从中得出任何结论。所以在这里发布我的具体错误。

4

1 回答 1

4

User.first返回nil所以你没有得到一个对象,因为数据库中没有对象!并且nil没有update_attributes方法,因为这是由ActiveRecord::Base. 您的问题是您的数据库中没有数据!

于 2012-01-17T07:17:36.550 回答