views:

141

answers:

2

I have a collection that contains instances of several different classes, and I want to render the partial for each instance. I can do this using the following code:

<%= render @results %>

My question is: How can I render the different partials in a different base directory? The above code will look for app/views/stories/_story.html.erb, however, the partials for this action are all kept in a different directory - app/search/_story.html.erb. Is there any way of specifying this?

+2  A: 

You could create a helper method like this:

def render_results(results)
  result_templates = {"ClassA" => "search/story", "ClassB" => "something/else"}
  results.each do |result|
    if template = result_templates[result.class.name]
      concat render(:partial => template, :object => result)
    end
  end
end

And then in the view call <% render_results(@results) %>

mutle
A: 

I have a similar situation where I have multiple classes so I use a partial for each class like:

for result in @results
  = render :partial => "result_#{result.class.to_s.downcase}", :locals => {:item => result}
end
Reuben Mallaby