0

I have a child object with an alternate partial form. Further to this question, I'm able to render the object using a partial form like this:

<%= render 'child_items/alternate_partial', :collection => parent_item.child_items %>

But I can't seem to access members of the collection in the partial. When I was using:

<%= render parent_item.child_items %>

the partial could just go <%= child_item.property %> and everything worked. But when I'm doing this the top way (and it seems that I have to, if I want to specify the partial), any reference to child_item throws a NameError. How do I get at the collection item inside the partial?

Community
  • 1
  • 1
Will Matheson
  • 338
  • 1
  • 4
  • 19

1 Answers1

0

As mentioned on http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials in 3.4.5 Rendering Collections:
When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial.

In this case, the partial is _alternate_partial, and within the _alternate_partial partial, you can refer to alternate_partial to get the instance that is being rendered.

Sergey Alekseev
  • 11,910
  • 11
  • 38
  • 53
  • Thanks. I tried that, when I try to access things I get `undefined method 'some_method' for nil:NilClass`. – Will Matheson May 30 '13 at 18:34
  • Ensure ```parent_item.child_items``` is present and you call ```<%= alternate_partial.property %>``` inside **child_items/alternate_partial**. I've just tried it myself and it works like expected. – Sergey Alekseev May 30 '13 at 18:40
  • `parent_item.child_items` is definitely present in the originating page. There was even some wacky thing I did where I printed the whole thing as a hash, but it was happening in the originating page, not the partial. By the way, does it matter that the originating page is itself a partial? I think I'm going to give up and render the child partials in the parent-partial. Makes it a one-stop-shop for the designers and stylers anyway. :-) – Will Matheson May 31 '13 at 11:42