9

I'm getting a really strange issue with a partial when trying to render a collection, I've even tried different approaches.

Here is my partial code (for debugging):

<pre><%= item.inspect -%></pre>

And here are my attempts to use it:

<%= render 'item', :collection => @foo.items %>
<%= render 'item', :collection => @foo.items, :as => :item %>

<% @foo.items.each do |item| %>
    <%= render 'item', :locals => {:item => item} %>
    <%= render 'item', :object => item %>
<% end %>

In each of those scenarios the partial just outputs nil, however if I pop a item.inspect inside my each loop the object details are displayed as expected.

The only thing I thought that could be a problem is that the items association is a short name mapped to a different class, so I thought that Rails 3 automagic thing might be assigning it to a variable to match that class name, however if I try and output that I get the 'undefined local variable error'.

I hope I'm overlooking something silly.

DEfusion
  • 5,533
  • 5
  • 44
  • 60

1 Answers1

10

Have you tried this already? — 

<% @foo.items.each do |item| %>
    <%= render 'item', :item => item %>
<% end %>

Update

Here's a guess for the collection:

<%= render :partial => 'item', :collection => @foo.items, :as => :item  %>
polarblau
  • 17,649
  • 7
  • 63
  • 84
  • I was pretty sure I had among all those variations I tried but I guess not as it seems to work. Still seems strange (and annoying) that I can't get collection to work though. – DEfusion Jan 17 '11 at 16:08
  • Well, good thing if it works. I've added something I'd still try concerning the collection. – polarblau Jan 17 '11 at 16:28
  • 6
    Wow if you don't include the :partial => 'item' part and use the new Rails 3 shorthand of just the partial name it doesn't work, but being specific makes it work. – DEfusion Jan 17 '11 at 16:36