Announcing inherit_views
February 23rd, 2007
Edge rails recently added support for multiple view paths. Very cool. This means you can ship a plugin complete with views, and have your controllers use those views (without copying them to app/views), and you get overriding for free.
inherit_views (svn and rdoc) is a plugin that allows you to do something similar, but within a controller hierarchy. (This is not a replacement, or duplication of 'multiple view paths')
Example
Here's an example of the syntax
class PostsController < ApplicationController inherit_views end class UserPostsController < PostsController end
UserPostsController will look for a particular view in app/views/user_posts, if it doesn't find it, then it looks in app/views/posts.
For the example above, you may wish to change some aspects of just index.rhtml, and leave the other views alone. Previously you would have to set up a bunch of erb (or haml, or whatever) files that either duplicate, or render the unchanged files. No longer.
render_parent
The inheritance model is familiar, so why not have the familiar 'super' construct in views?. You can now:
posts/_resource.rhtml:
<%= post.title %>
<%= link_to 'comments', comments_path(post) %>
user_posts/_resource.rhtml:
<%= render_parent :locals => {:post => post} %>
<%= link_to 'my comments', user_comments_path(@user, post) %>
Why?
- Making life for STI users (I'm a heavy user) easier. This is a classic case where inheriting views makes life easier. It's also easier if you use something like resources_controller to decouple the model name form the view (instead of @my_model - which then becomes @my_model_subclass, use a local, or in the case of resources_controller resource)
- Providing the basic tools to build auto-scaffold type systems. I think the idea of these systems is great - but they never work quite right for me. For me, they cover too much ground, make too many assumptions. So, I'd like to have a set of tools that help me easily build one of these auto-scaffold things for use in just one, or a few projects. inherit_views and resources_controller are two such tools.
Two main reasons:
April 18th, 2007 at 12:39 PM Comments are now working again!
April 19th, 2007 at 05:40 PM very nice plugin! just one question: can it be, the "new" views are not inherited? all works for me, except "new.rhtml". i get a ActionController::UnknownAction (No action responded to new) - if i create a file new.rhtml in the child view-directory, it displays it. but "new" seems to get left out when inheriting.
October 3rd, 2007 at 02:22 PM
No probs with new.rhtml.
But what about .rjs-files? They are not inherited.
October 4th, 2007 at 09:29 AM They are for me - (edge rails). Can you give me some more info please about your installation?
November 5th, 2007 at 10:41 PM Great plugin. Struggling with one aspect. If you had specific show actions for each controller subclass, how would you build the url to get to the specific show from outside that controller subclass' context?
April 2nd, 2008 at 10:05 AM No probs with new.rhtml. But what about .rjs-files? They are not inherited.