View inheritance (sometimes called template inheritance) is a neat feature of Rails/ActionController that seems to be often neglected, poorly understood or passed over as not being useful — but when used properly it can really help DRY up and unclutter your views.
The way it works is simple enough: when checking the filesystem for a template or partial, Rails will walk up the inheritance hierarchy of the controller until it finds something. For example, consider the following:
class AuthenticatedController < ApplicationController
class UsersController < AuthenticatedController
# some awesome code...
When rendering a partial — let's say
_table.html.erb, the filesystem will be checked in the following order:
The first file found will be used. The same goes for templates (such as
All is well and good until you find yourself in a situation where direct inheritance doesn't make sense. If you've made use of the
concerns directory and abstracted out chucks of reusable controller code (nice work) — effectively multiple inheritance — you'll find you loose the functionality above.
The culprit is the
#parent_prefixes method, which recurses the inheritance hierarchy using
#superclass. Fear not — it's easy to mess with:
@parent_prefixes ||= super.unshift('authenticated')
module UsersController < ApplicationController
# the same awesome code...
I should mention that it looks like this method of overriding
#parent_prefixes is slated for the chopping block — it will be replaced with
#local_prefixes in Rails 4.3 and 5.0.