Two layouts, one line that switches between them
Two layout files, both rendering pages in this app
app/views/layouts/application.html.erb and app/views/layouts/admin.html.erb. Every public page, such as the roadmap, a course or a lesson, is wrapped in the first. Every page under /admin, such as the dashboard or the course editor, is wrapped in the second: a sidebar-driven layout with no navbar or footer. (Emails use a third pair, mailer.html.erb and mailer.text.erb, through layout "mailer" in ApplicationMailer, which this course leaves aside.) Two different sets of page chrome, and this course is about the line of code that decides which controller gets which.
The one line, from this app's file
# app/controllers/admin/base_controller.rb
module Admin
class BaseController < ApplicationController
before_action :require_login
before_action :require_admin
helper Admin::PaginationHelper
layout "admin"
end
end
layout "admin": one word, one string. Every controller in this app's Admin:: namespace, from Admin::Catalog::CoursesController to Admin::Enrollment::BadgesController, inherits from Admin::BaseController, and none of them repeats this line. ApplicationController, the base every other controller inherits from, has no layout call at all. One declaration, one namespace boundary, two wrapping templates.
Why "namespacing" is the right word
This app's admin area isn't a separate application. Admin::Catalog::CoursesController uses the same Catalog::Course model, database and session as the public Catalog::CoursesController a student browses. What changes at the Admin:: boundary is presentation, a different layout, plus two before_actions in the same file (require_login, require_admin) that the public side doesn't have. Here the namespace is more than a Ruby module for organizing files. It's the boundary where both "who may see this" and "what it looks like" change together.
What this course covers
Seven lessons, each checked against this app's two layout files and its controller hierarchy. Lesson 2 covers what the layout class method does and where it's declared. Lesson 3 shows how Admin::Catalog::CoursesController, nested two levels deep, inherits it without redeclaring anything, using Rails' installed source. Lesson 4 shows how ApplicationController, with no layout call, still ends up with layouts/application.html.erb by convention. Lesson 5 compares the two files line by line. Lesson 6 covers the layout method's other forms (a Symbol, a Proc, false) and which of them this app uses, including one it uses in an unexpected place.
Keep going with lesson 2
The layout class method, declared once in Admin::BaseController
Explains layout as a class method from ActionView::Layouts that records configuration and renders nothing by itself.
A free account also gives you the quizzes, your progress and notes, and the discussion.