Rails 中未定义的方法 [] 意味着什么?

发布于 2024-09-25 11:33:49 字数 389 浏览 2 评论 0原文

我收到以下错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

↘人皮目录ツ 2024-10-02 11:33:50

我在您的代码中看到一个错误,除非应该启动一个块,否则,就像它所写的那样,该行什么也不做。
所以这应该有效:

 <%=h @contact.date_entered.to_date %></br>  
 Next event: 
 <% next_delayed_todo = @contact.next_delayed_todo %> 
 <% unless next_delayed_todo[:event].nil? do %>
   <%= next_delayed_todo[:event].title %> </br>
 <% end %>

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:

 <%=h @contact.date_entered.to_date %></br>  
 Next event: 
 <% next_delayed_todo = @contact.next_delayed_todo %> 
 <% unless next_delayed_todo[:event].nil? do %>
   <%= next_delayed_todo[:event].title %> </br>
 <% end %>
桃扇骨 2024-10-02 11:33:49

这意味着您在 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?

秉烛思 2024-10-02 11:33:49

这意味着 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

柠北森屋 2024-10-02 11:33:49

看看你的视图中如何有 3 行连续的代码,除了代码之外什么都没有?这表明您应该将其拉出到助手中,以保持您的视图干净。

新视图代码:

<%=h @contact.date_entered.to_date %></br>  
Next event: <%= next_delayed_todo(@contact) %> </br>

然后在您的帮助程序中:

def next_delayed_todo(contact)
  contact.next_delayed_todo[:event].title rescue ""
end

请注意,您收到的错误是因为 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:

<%=h @contact.date_entered.to_date %></br>  
Next event: <%= next_delayed_todo(@contact) %> </br>

Then in your helper:

def next_delayed_todo(contact)
  contact.next_delayed_todo[:event].title rescue ""
end

Note that the error you were getting is because of next_delayed_todo being nil. The first line of the helper method uses rescue "" to set an alternate value if it is nil. You can replace it with rescue "none." or any other string that makes sense.

执笏见 2024-10-02 11:33:49

NoMethodError:是一个从异常派生的类,是一个异常。在强动态类型语言中,如果您尝试在不存在该方法的类型上使用该方法,则会抛出错误的类型异常。

method_missing 是一个函数
-http://ruby-doc.org/core/classes/Kernel.html#M005925

当 obj 被发送时由 Ruby 调用
它无法处理的消息。符号是
所调用方法的符号,以及
args 是任何参数
传递给它。默认情况下,
当这个时解释器会引发错误
方法被调用。然而,它是
可以重写该方法
提供更动态的行为。这
下面的示例创建了一个 Roman 类,
它响应带有名称的方法
由罗马数字组成,
返回对应的整数
值。

我认为(不要相信我这一点)默认的 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

Invoked by Ruby when obj is sent a
message it cannot handle. symbol is
the symbol for the method called, and
args are any arguments that were
passed to it. By default, the
interpreter raises an error when this
method is called. However, it is
possible to override the method to
provide more dynamic behavior. The
example below creates a class Roman,
which responds to methods with names
consisting of roman numerals,
returning the corresponding integer
values.

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}".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文