Hi, 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>
-
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?
Angela : Hi, I do have the following code a few lines up: <% unless next_delayed_todo.nil? || next_delayed_todo[:event].nil? %> hmm...so not exactly sure what to do...Angela : ah, interesting...it's in my local but not on heroku I guess...let me make some adjustments.From kwerle -
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
From Rutger -
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 "" endNote that the error you were getting is because of
next_delayed_todobeing 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.Angela : although didn't answer the question, this is helpful change I need to make...thanks +1Mark Thomas : It did have a *solution* to your question, but I have added a note that makes it explicit.Angela : ah, yes -- the rescue also is a key component to the solution I see....From Mark Thomas -
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}".
From Roman A. Taycher -
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 %>From nathanvda
0 comments:
Post a Comment