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
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 lessonsThe file wrapping every view
Shows how app/views/layouts/application.html.erb supplies the html tag, head, navbar and footer around every view.
Bare yield: inserting the current view
Explains how bare yield inserts the current action's rendered view inside main, with everything else shared.
yield(:name) and content_for
Covers yield(:name), content_for and content_for?, which let views fill optional slots that may fall back to defaults.
Ruby's yield vs. this yield: the same keyword
Uses the Rails 8.1 source to show that the layout's yield is Ruby's keyword calling a Rails-built block.
This project's real layout, start to finish
Reads the project's layout from start to finish, covering its four yield calls and their different fallbacks.
content_for from a real view
Follows real views that fill :title, :body_class and :main_class, linked to the layout only by shared names.
Putting layouts together
Reviews yield, named slots and content_for together, and notes the render calls the layout still contains.