当我不期望它时,我得到一个 nil 对象引用 -ROR
通过以下命令从 Rails 控制台更新数据库时:
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,这与教程相关。我发现网站上已经回答了类似的问题,但无法从中得出任何结论。所以在这里发布我的具体错误。
while updating a database from rails console by following commands:
user=User.first
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1
=> nil
And then when I update it using
user.update_attributes(:email => "[email protected]", :password => "foobar", :password_confirmation => "foobar")`
I get this error:
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
I am trying to learn ROR from mhartl tutorial and this is related to the tutorial. I found similar questions already answered on the site but couldn't make anything out of them. So posting my specific error here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
User.first
返回nil
,因此您不会获得对象,因为数据库中没有对象!并且nil
没有update_attributes
方法,因为它是由ActiveRecord::Base
继承的。你的问题是你的数据库中没有数据!The
User.first
returnsnil
so you don't get an Object because there is no object in the database! Andnil
doesn't have anupdate_attributes
methods because this is inherited byActiveRecord::Base
. Your problem is that there is no data in your database!