Intern Track Roadmap

ActionView: layouts & yield

This course explains how a Rails layout wraps every view. It starts with app/views/layouts/application.html.erb, the file that holds the html tag, head, navbar and footer shared by every page. Bare yield inserts the current view's rendered output. Named yield(:name) reads optional slots that views fill with content_for, and content_for? lets a slot fall back to a default. The lessons compare this yield with Ruby's block keyword using the Rails 8.1 source. They then read the project's real layout and a view that fills its slots.

What you'll learn

  • ✓What a layout file is and how it wraps every view
  • ✓How bare yield inserts the current action's rendered view
  • ✓How yield(:name) and content_for fill optional named slots
  • ✓How content_for? gives a slot a fallback default
  • ✓Why the layout's yield is the same Ruby keyword that calls a block
  • ✓How the project's real layout and views connect through shared slot names
Free preview · Lesson 1 2 min read

The file wrapping every view

partners.html.erb, opened in the last course, never mentions an <html> tag, a <head>, a navbar or a footer. Visit /partners in a browser and all of that is there, the same header and footer every other page shares. That missing half lives in one file, app/views/layouts/application.html.erb, the…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...