Reputation: 43996
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?
Upvotes: 1
Views: 1231
Reputation: 5353
or you can use is_a?(Object)
if is_a?(classA)
render something_A
elsif is_a?(classB)
render something_B
end
Upvotes: 2
Reputation: 5767
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
Upvotes: 1
Reputation: 46
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) %>
Upvotes: 2