开发者

Rails 3 Render Partial nil object

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 Pa开发者_如何学编程rtial:

<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 @status in the ERB partial, where as you should be referring to status, without the leading @.

The @status means to look for a defined instance variable. While passing in the :status => s means that the view will have access to s via a local variable named status.

Also, if simply :status => s does not work, try :locals => {:status => s}. In any case, access status in the view, not @status.


The biggest problem is that :status is an option for render. It renders the http code like 200 or :ok.

What should work is:

<%= render :partial => 'statuses/show', :locals => { :status => s } %>

and don't use the @ for the variable.

<%= status.title %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜