0

I want to render some child items with something other than their default partial (i.e., app/views/child_items/_child_item.html.erb). The default one was scaffolded and it isn't great for public viewing of something, but I still want to keep it for back-end management purposes.

This is what I'm going for inside the view of the parent item, assuming a partial defined in app/views/child_items/_alternate_partial.html.erb:

<%= render containing_object.child_items, :partial => 'child_items/alternate_partial' %>

But the child items still render with their default partial.

Will Matheson
  • 338
  • 1
  • 4
  • 19

1 Answers1

1

Try this one:

<%= render 'child_items/alternate_partial', :collection => containing_object.child_items %>
Sergey Kuznetsov
  • 8,591
  • 4
  • 25
  • 22
  • Ok, that got me into the partial! Now I get a NameError that I didn't before - it doesn't know what a `child_item` is. – Will Matheson May 30 '13 at 15:39
  • Inside the partial code you should use variable named like a class of the objects you're rendering. For example, if your child_item object is an instance of a Post model, you should use `post` in your `alternate_partial` instead of `child_item` – Sergey Kuznetsov May 30 '13 at 16:09
  • Check this http://guides.rubyonrails.org/layouts_and_rendering.html (section 3.4.5 and 3.4.6) to get more information about how to work with rendering collections in partials. – Sergey Kuznetsov May 30 '13 at 16:11
  • What I want to understand is how to access an item in the collection we passed into the partial. The default partial could simply go child_item.some_object, why doesn't the same approach work here? – Will Matheson May 30 '13 at 16:41
  • No luck so far, but I'll ask it as a separate question. At least I got into the partial. – Will Matheson May 30 '13 at 17:16