what do an undefined method [] mean in rails?

谁说我不能喝 提交于 2019-12-25 08:27:55

问题


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>

回答1:


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.




回答2:


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?




回答3:


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




回答4:


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




回答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:

 <%=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 %>


来源:https://stackoverflow.com/questions/3835428/what-do-an-undefined-method-mean-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!