skip to content

In a Rails app, how does an action's view end up inside app/views/layouts/application.html.erb, and what does yield do there?

level: juniorimportance: must knowfreq 68%

answer

  1. template first, layout second
  2. layout named after the controller
  3. falls back to layouts/application
  4. bare yield is the view's output
  5. render plain: skips the layout

basics

~10 s

In Rails, Action View renders the action's template first, then renders the controller's layout, by default layouts/<controller> or else layouts/application, and the layout's bare yield inserts the template's HTML at that point.

solid answer

~30 s

When a Rails controller renders an HTML template, Action View renders the template first and stores its output, then finds a layout: one named after the controller, such as `layouts/exhibits.html.erb` for `ExhibitsController`, otherwise one inherited from a parent controller, which in a new app is `layouts/application.html.erb`. The layout is an ordinary ERB template; a bare `<%= yield %>` prints the stored template output, and `yield :name` prints a named region a view filled with `content_for`. Layout and view share the controller's instance variables. Responses such as `render plain:` or `render json:` get no layout unless one is asked for with `layout:`.

code

erb · 13 lines
erb
<%# app/views/layouts/application.html.erb %>
<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for(:title) || "City Museum" %></title>
    <%= csrf_meta_tags %>
    <%= yield :head %>
  </head>
  <body>
    <%= render "shared/nav" %>
    <main><%= yield %></main>
  </body>
</html>

go deeper

for a junior

Know that layouts live in app/views/layouts, that application.html.erb is the default, and that a bare yield inserts the page.

for a middle

Explain the render order, the lookup by controller name with inheritance, and which renders skip the layout.

for a senior

Use the render order and lookup rules to debug a page with the wrong shell or a missing title without guessing.

for a principal

Keep the number of layouts small and their regions documented, so new pages compose instead of copying shells.

## What a layout is A **layout** is the HTML shell shared by many pages: doctype, `<head>`, navigation, footer. In Rails it is an ordinary template under `app/views/layouts/`, and it says where the page-specific content goes with `yield`. A new Rails 8.1 app ships `app/views/layouts/application.html.erb` with a `<title>` that reads `content_for(:title)`, the CSRF and CSP meta tags, a `yield :head` slot, the stylesheet tag and `<%= yield %>` in the body. ## The order of rendering Rendering an action with a layout happens in two steps: 1. Action View renders the **action's template**, for example `exhibits/show.html.erb`, including every partial it renders. Calls to `content_for` inside it store named regions as they run. 2. It stores that output as the unnamed region and renders the **layout**. Each `yield` in the layout reads a region: a bare `yield` returns the template's output, `yield :head` returns whatever was stored under `:head`. Because the template finishes first, values a view sets, such as a page title through `content_for` or an instance variable, are ready by the time the layout's `<head>` is rendered, even though the head comes first in the HTML. ## How the layout is chosen by default | Controller | Layout looked for first | Then | |---|---|---| | `ExhibitsController < ApplicationController` | `layouts/exhibits` | `layouts/application` (from the parent) | | `Staff::ExhibitsController < ApplicationController` | `layouts/staff/exhibits` | `layouts/application` | | `Staff::ExhibitsController < Staff::BaseController` | `layouts/staff/exhibits` | `layouts/staff/base`, then `layouts/application` | The lookup uses the same format as the response, so an HTML request uses `application.html.erb`. A controller can override all of this with the `layout` macro, and a single render can pass `layout:`. ## When no layout is applied - `render plain:`, `render html:`, `render body:` and `render inline:` skip the layout unless the call also passes `layout:`. - `render json:` produces a body with no template, so there is nothing to wrap. - `render partial:` from a controller renders just that partial, with no layout unless `layout:` is passed. - A layout that only exists as `.html.erb` is not used for a JSON template; the lookup asks for the response's format. ## Variables and helpers The layout runs in the same view context as the template, so: - instance variables set by the controller, or by the template, are visible in the layout; - helpers such as `link_to`, `current_page?` and the app's own helpers work the same way; - escaping rules are the same: `<%= @exhibit.title %>` in the layout is escaped like anywhere else. Setting an instance variable in a view so the layout can read it works, but `content_for` is the convention for page-to-layout data, because it is explicit about which regions exist. ## Slips that show up in review - `<% yield %>` without the equals sign runs the lookup and prints nothing, so the page body vanishes. - An action template that repeats `<html>` and `<body>` ends up with two documents nested inside each other. - A layout saved without a format, such as `layouts/application.erb`, matches every format, so it can end up wrapping a JSON template too; name it `application.html.erb`. - A missing `<%= yield :head %>` in a custom layout silently drops every page's extra head tags. ## A museum example The public museum site uses `application.html.erb`. An exhibit page sets its title with `content_for :title, @exhibit.title`, renders its body, and the layout prints `content_for(:title) || "City Museum"` in the `<title>` and the page body at `<%= yield %>`. A gallery-shop controller with its own `layouts/shop.html.erb` gets that shell automatically, with no code in the controller.

  • Why can a page title set at the bottom of `exhibits/show.html.erb` still appear in the layout's `<head>`?
    Rails renders the whole action template before it starts the layout. Every `content_for` call in the template, wherever it sits, has run by the time the layout's head is rendered, so `content_for(:title)` already holds the value.
  • Which layout does `render plain: "ok"` use in a controller whose views use `application.html.erb`?
    None. Plain, html, body and inline renders skip the layout by default; passing `layout: true` or a layout name to that render applies one.

  • Rails renders the layout first and then streams the template into it.
  • Every controller needs a layout macro to use application.html.erb.
  • Instance variables set in the controller are not visible in the layout.
  • render plain: wraps the text in the application layout.
  • A layout can contain only one yield call.