Rails 中未定义的方法 [] 意味着什么?
我收到以下错误:
undefined method `[]' for nil:NilClass
第 50 行的相关片段:
47: <%=h @contact.date_entered.to_date %></br>
48: Next event:
49: <% next_delayed_todo = @contact.next_delayed_todo %>
50: <% unless next_delayed_todo[:event].nil? %>
52: <%= next_delayed_todo[:event].title %> </br>
I get the following error:
undefined method `[]' for nil:NilClass
The related snippet in line 50:
47: <%=h @contact.date_entered.to_date %></br>
48: Next event:
49: <% next_delayed_todo = @contact.next_delayed_todo %>
50: <% unless next_delayed_todo[:event].nil? %>
52: <%= next_delayed_todo[:event].title %> </br>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在您的代码中看到一个错误,除非应该启动一个块,否则,就像它所写的那样,该行什么也不做。
所以这应该有效:
I see an error in your code, unless should start a block, otherwise, like it is written the line does nothing.
So this should work:
这意味着您在 nil 对象上调用了 []。在这种情况下,next_delayed_todo 为零。
你想要的东西更像是:
除非 next_delayed_todo.nil? || next_delayed_todo[:event].nil?
It means that you called [] on a nil object. In this case, next_delayed_todo was nil.
You want something more like:
unless next_delayed_todo.nil? || next_delayed_todo[:event].nil?
这意味着 NilClass 没有实现 [] 方法。这反过来意味着,在您的代码中,next_delayed_todo 为零。
.nil?您现在检查 next_delayed_todo[:event] 返回的值是否为零。您还应该为 next_delayed_todo 添加 nil 检查
It means that the NilClass doesn't implement the [] method. Which in turn means that, in your code, next_delayed_todo is nil.
the .nil? you have now checks if the value returned by next_delayed_todo[:event] is nil. You should also add a nil check for next_delayed_todo
看看你的视图中如何有 3 行连续的代码,除了代码之外什么都没有?这表明您应该将其拉出到助手中,以保持您的视图干净。
新视图代码:
然后在您的帮助程序中:
请注意,您收到的错误是因为
next_delayed_todo
为零。辅助方法的第一行使用rescue ""
设置替代值(如果该值为零)。您可以将其替换为rescue "none."
或任何其他有意义的字符串。See how you have 3 consecutive lines of nothing but code in your view? That's a sign you should pull it out into a helper, in order to keep your views clean.
New view code:
Then in your helper:
Note that the error you were getting is because of
next_delayed_todo
being nil. The first line of the helper method usesrescue ""
to set an alternate value if it is nil. You can replace it withrescue "none."
or any other string that makes sense.NoMethodError:是一个从异常派生的类,是一个异常。在强动态类型语言中,如果您尝试在不存在该方法的类型上使用该方法,则会抛出错误的类型异常。
method_missing 是一个函数
-http://ruby-doc.org/core/classes/Kernel.html#M005925
我认为(不要相信我这一点)默认的 method_missing 函数会执行类似于 NoMethodError 的操作,并显示消息“未定义方法‘#{method_name}’ for #{inspectVariableValue}:#{ClassName}”。
NoMethodError: is an class derived from exception, is an exception. In a strong dynamically typed language if you try to use a method on types for which that method doesn't exist you throw a bad type exception.
method_missing is a function
-http://ruby-doc.org/core/classes/Kernel.html#M005925
I think(don't trust me on this) the default method_missing function does something like a NoMethodError with the message "undefined method `#{method_name}' for #{inspectVariableValue}:#{ClassName}".