0

I am banging my head to figure out a way to have a heterogeneous collection of partials. I use this to list the collection of partials called @feed_items (in the "Tweet" class) and its working great for the one class:

  <%= render :partial => 'shared/feed_item', :collection => @feed_items %>

@feed_promos partials have objects of a different class ("promotion" class) and they are in the same 'shared' directory as feed_item partial. Does anyone know the code to have both @feed_promo and @feed_item in the collection?

I have tried to simplify using the rails 3 shortcut this:

  <%= render [feed_items, feed_promos] %>

But I get an error because it expects to find a template for the Tweet model but its not there and I want a collection of partials, not the objects of the different classes (like Tweets). I have already gone into the relationship.rb and tweet.rb and have included:

def to_partial_path 'shared/'
Tim
  • 99
  • 1
  • 11

1 Answers1

0

Put your partials respectively in tweets/_tweet.html.erb and promotions/_promotion.html.erb

In your controller make a single collection:

@feed = @feed_items + @feed_promos

In your view:

<%= render @feed %>
Thomas Guillory
  • 5,719
  • 3
  • 24
  • 47
  • Unfortunately it didnt work. The + was not recognized in the controller so I changed that to '&&'. Is that right? Also, do you mean '_tweet.html.erb' for the name of the partials rather than '_tweet.rb'? I tried making these changes but still got an ArgumentError on the page saying: 'nil' is not an ActiveModel-compatible object that returns a valid partial path. – Tim Mar 29 '12 at 03:39
  • Sorry for the mistake on the extension, it's edited. What are the types of `@feed_items` and `@feed_promos` ? The `+` operator merge two arrays in one. It works fine on my computer. The `&&` is wrong: it's a logical AND and returns nil or the second operand. You get an error because `@feed` is `nil` and not an `Array`. – Thomas Guillory Mar 29 '12 at 14:06