Template Inheritance
Rails 3.1
Now that the internals of Rails 3 are a little more hospitable to changes we now have a long-standing feature request finally implemented - inherited templates. As the name implies, inherited templates make template lookup follow the controller inheritance heirarchy if it can’t find a template for the current controller. This is probably best described with a basic code example. Assuming we have the following controller heirarchy (basically PostsController inherits from AssetsController):
class AssetsController < ApplicationController
# Assume index, show etc... action definitions
end
class PostsController < AssetsController
# Assume index, show etc... action definitions
end
Assume there are asset templates for show and index but only an index template for posts:
myapp > ls app/views/assets
index.html.haml show.html.haml
myapp > ls app/views/posts
index.html.haml
In this scenario any request to the show action of the posts_controller will be rendered using the views/assets/show template. index requests will be rendered as expected since both controllers have their own index templates.
Template inheritance also applies to partial lookups and does not touch layout lookup which is already based on heirarchical lookup. (So basically all template types have inherited lookups.)
While a seemingly small feature this new inherited lookup of templates allows you to reuse whole parts of common view logic without resorting to using an amalgamation of partials to accomplish the same thing.