I feel like I’m doing something dumb here. I’m making a simple TODO list app (I realize there are already a million of them). I have a project definition and the various statuses that a task within in that project can be assigned.
No matter what, I’m receiving the following error message when hitting the page:
undefined method `title’ for nil:NilClass
This question about nil objects is extremely similar, however, the suggested solutions don’t seem to fix the issue I’m encountering. I only have one status associated with the project and it is not nil. I’m wondering if this issue has anything to do with the statuses being an association…
In my project I have:
<% @project.statuses.each do |s| %>
<%= s.inspect %>
<%= render 'statuses/show', :status => s %>
<% end %>
#if I take out the render line - the status shows up
The status right now is basically just a generic scaffolded view — if I was passing the data from a controller directly it would receive an object @status.
…
I have tried this with render :partial..., :locals => { :status = s} etc. etc. Any suggestions would be appreciated.
Update – Added _show Partial:
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= @status.title %>
</p>
<p>
<b>Description:</b>
<%= @status.description %>
</p>
<p>
<b>Active:</b>
<%= @status.active %>
</p>
<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %>
Update – Added more of the error message
NoMethodError in Projects#show
Showing /home/.../app/views/statuses/_show.html.erb where line #5 raised:
undefined method `title' for nil:NilClass
I believe the problem is that you’re referring to
@statusin the ERB partial, where as you should be referring tostatus, without the leading@.The
@statusmeans to look for a defined instance variable. While passing in the:status => smeans that the view will have access tosvia a local variable namedstatus.Also, if simply
:status => sdoes not work, try:locals => {:status => s}. In any case, accessstatusin the view, not@status.