Skip to content

Instantly share code, notes, and snippets.

@ecoologic
Last active August 29, 2015 14:05
Show Gist options
  • Save ecoologic/7ac31d3f9e20b604d35d to your computer and use it in GitHub Desktop.
Save ecoologic/7ac31d3f9e20b604d35d to your computer and use it in GitHub Desktop.
Template Inheritance

Similarly to the Layout Inheritance logic, if a template or partial is not found in the conventional path, the controller will look for a template or partial to render in its inheritance chain. For example:

# in app/controllers/application_controller
class ApplicationController < ActionController::Base
end

# in app/controllers/admin_controller
class AdminController < ApplicationController
end

# in app/controllers/admin/products_controller
class Admin::ProductsController < AdminController
  def index
  end
end

The lookup order for a admin/products#index action will be:

  • app/views/admin/products/
  • app/views/admin/
  • app/views/application/

This makes app/views/application/ a great place for your shared partials, which can then be rendered in your ERb as such:

<%# app/views/admin/products/index.html.erb %>
<%= render @products || "empty_list" %>

<%# app/views/application/_empty_list.html.erb %>
There are no items in this list <em>yet</em>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment