New Feature for Rails 2.0: Multiple Controller View Paths

Posted by rick February 04, 2007 @ 11:12 PM

Thanks to John Long, Rails now supports multiple view paths for each controller. For 90% of the apps, this probably won’t make a huge difference. But, for those of you wanting to productize various open source apps, you can now do this:


/beast/trunk$ mkdir vendor/plugins/test_view_paths
/beast/trunk$ cd vendor/plugins/test_view_paths
/beast/trunk/vendor/plugins/test_view_paths$ mkdir views
/beast/trunk/vendor/plugins/test_view_paths$ echo "ActionController::Base.view_paths.unshift File.join(directory, 'views')" > init.rb

What you’ve done is created a minimal rails plugin that inserts a ‘views’ directory in the plugin to the beginning of the #view_paths array. Now, copy a view from the rails app into the plugin’s ‘views’ directory, make a noticeable change, and start your app. You’ll notice that it picked your plugin’s view over the application’s view.

Posted in Documentation, Edge | 11 comments

Comments

  1. martin on 04 Feb 23:57:

    cool. first words about rails 2.0 :D~

  2. rick on 05 Feb 01:46:

    Well, anything in Edge now that’s not in 1.2 is a Rails 2.0 feature.

  3. Warren Konkel on 05 Feb 02:45:

    I strongly feel that plugin views should be in vendor/plugins//app/views and not in vendor/plugins//views. Doing this would follow the same conventions a standard rails app has and will pave the way for putting controllers, helpers and models in plugins as well.

  4. Warren Konkel on 05 Feb 02:47:

    Silly asterisk to bold conversion… what I meant to say was:

    ... should be in vendor/plugins/PLUGIN/app/views and not in vendor/plugins/PLUGIN/views…

  5. rick on 05 Feb 05:38:

    It’s a plugin, do whatever you want. I was going for a simple example.

  6. Sean Cribbs on 05 Feb 13:50:

    I have worked with John on getting out the 0.6 release of RadiantCMS, which includes this enhancement (as part of Radiant, not a modified version of Rails). Hopefully next up will be multiple fixture paths!

  7. Thijs on 05 Feb 13:56:

    Multiple migration paths would be great to!

  8. Jason Perry on 05 Feb 15:24:

    FInally! I can abandon my forked version of Collaboa! Thank you so much.

  9. Dave on 05 Feb 16:55:

    Awesome

    Will this be available as a plugin to start using right away?

  10. James Adam on 05 Feb 19:57:

    There are already a couple of plugin implementations that supply this functionality – the engines plugin (new “cast any preconceptions aside” release, as of yesterday), and “plugin_routing” from pluginaweek.org, for example.

  11. Spyros on 20 Feb 13:14:

    10 yep. pretty much an engine feature.