Alex Smith
Alex Smith

Reputation: 385

Partials for Collection of Rails Objects

I have a collection of 3 types of objects, Posts, Products, and Content. I want to put all of these objects into a feed and order them by when they were created. I've done all that. The issue I'm having is I want to have a different partial for each item. How would I do that?

Here's the controller action getting all the objects:

 @stream = current_user.stream

Upvotes: 0

Views: 45

Answers (1)

Anezio Campos
Anezio Campos

Reputation: 1555

You can try something like this if each item has it own class:

@stream.each do |item|    
  render item
end

Or this if there is a field "kind" for the objects:

@stream.each do |item|    
  render item.kind
end

and create the following partials:

_post.html.erb
_product.html.erb
_content.html.erb

Just be sure that those files are on the correct view folder

Upvotes: 1

Related Questions