在 Rails 3 中调用 id 为 nil [英] Called id for nil in Rails 3
本文介绍了在 Rails 3 中调用 id 为 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
处于开发模式:
nil.id
=> "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"
在生产模式:
nil.id
=> 4
为什么?
推荐答案
在您的环境配置中查找以下行:
Look for the line that says the following in your environments configs:
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true # or false in production.rb
这是为了防止您在开发模式下调用 nil
上的方法.我猜他们出于生产中的性能原因禁用了它.
This is to prevent you from calling methods on nil
while in development mode. I guess they disabled it for performance reasons in production.
而且 nil
在 ruby 中是一个单例对象,这就是为什么它的 id
无论如何都是 4.
And nil
is a singleton object in ruby, that's why its id
will be 4 no matter what.
这篇关于在 Rails 3 中调用 id 为 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文