Darwinweb

Multiple Controller View Paths Enhanced!

June 6, 2007     

I don’t know about you, but I was extremely excited to hear about the new Multiple Controller View Paths
in Rails 2.0. I’ve been hacking around with theme_support and looking at liquid and engines for implementing one huge feature—the ability to customize an application that is served to multiple domains. Although I plan to implement Liquid eventually, at this stage it’s just too time-consuming to set up the necessary definitions. In the end I made some patches to theme_support and dropped it in. It worked, but theme_support requires getting pretty dirty inside ActionPack (ever wonder why it breaks on every new Rails release?).

With multiple view_paths instead of one template_root all the ugliness evaporates. I was able to write a multisite plugin (I will release this shortly) by cutting out 95% of theme_support and adding a simple macro to ActionController::Base. So I really have to thank John Long for this very elegant patch. This is a critical feature to have in core, because it is overall pretty simple, but requires numerous tiny changes throughout ActionPack.

I’ve moved my app to edge to take advantage of this functionality, and discovered one slight drawback—the view_paths can only be set at initialization time. Although this can be worked around, I think there’s a strong case for being able to set the view_paths on a per-request basis. In my case that is domain-dependent customization, but I’m sure there are plenty of other use-cases (anyone?). The only thing preventing this is that ActionView maintains a separate view_paths variable from ActionController, so I went ahead and submitted a patch to remedy this issue. If this is something that you would find useful, or if you can think of a better solution, please chime in on the ticket.