Reputation: 9779
I have a collection, @comments, that is heterogeneous but hierarchical. Each comment is either an instance of Comment or some derived class, like ActionComment or InactionComment. I am rendering a different partial for each type of Comment. The View code is:
= render @comments
As all the partials are related, I would like to keep them in a single view directory, i.e.:
But right now in order to use the automatic rendering of the correct partial, I am using separate directories, like:
Upvotes: 2
Views: 693
Reputation: 1823
Rails 3.2 makes a Model#to_partial_path method available which allows you (as its name suggests) to override the partial pathname.
def to_partial_path
self.action.to_s
end
The path it returns does not include the leading underscore and is assumed to be relative to .../views/modelname/
. See http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/ for an overview
Upvotes: 4
Reputation: 5595
You can't do it quite as magically, but you can do it by just rendering each item individually and specifying the partial.
example in haml:
- @comments.each do |c|
= render :partial => "comments/#{c.class.to_s.underscore}", :locals => {:comment => c}
Upvotes: 1