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?

Two main reasons:

  1. 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)
  2. 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.

6 Responses to “Announcing inherit_views”

  1. Ian Says:
    Comments are now working again!
  2. parasew Says:
    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.
  3. role Says:

    No probs with new.rhtml.
    But what about .rjs-files? They are not inherited.

  4. Ian Says:
    They are for me - (edge rails). Can you give me some more info please about your installation?
  5. steve Says:
    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?
  6. resimler Says:
    No probs with new.rhtml. But what about .rjs-files? They are not inherited.

Leave a Reply