Views: ERB Templates & the Rails MVC
Website Rebuild with Ruby on Rails
Chapter 4 · Views: ERB Templates & the Rails MVC
A Rails Controller action renders a view — the same MVC shape as every sibling course. The template layer, ERB, is where this chapter finds its own genuinely distinctive material: not in layouts and partials, which every sibling already has an equivalent of, but in exactly how much of the host language ERB lets through, and in a dedicated architectural layer — Helpers — the other three siblings don't offer quite the same way.
Layouts: The Same Wrapping Shell, ERB's Own Syntax
<%= yield %> is Rails' own equivalent of Blade's @yield and Django's {% block %} — the point where the current page's own template content is dropped into the shared shell. <%= %> outputs the expression's result into the page; plain <% %> executes Ruby without outputting anything — used for control flow like loops and conditionals.
Partials: Reusable View Fragments
<%== %> outputs without HTML-escaping — ERB's own direct equivalent of Blade's {!! !!} and Django's |safe filter, needed here for the same reason: @page.body is trusted, already-formatted HTML content, not user input that needs escaping.
A Third Place for View Logic: Helpers
Called from the view as breadcrumb_for(@page).each do |crumb| ... end. Rails ships app/helpers/ by default, and every method defined there is automatically available in every view — a dedicated architectural layer specifically for view-supporting logic that's too presentational for a model method but too structured to sprawl across the template itself.
@php would technically have allowed it inline. Rails offers a genuinely separate, named layer for exactly this kind of logic — Helpers aren't a workaround; they're a first-class, intended part of the MVC structure.
breadcrumb_for walks current.parent in a loop — one query per level, exactly the same N+1 pattern both sibling courses' own Chapter 4/6 built into their breadcrumb code on purpose. It's left as written here, to be caught and fixed live in Chapter 6, matching the running motif across the whole series.
ERB's Permissiveness, Verified Honestly
ERB has no separate template language layered on top of Ruby the way Django's DTL is a genuinely separate, deliberately restricted language — <% %> tags contain real, unrestricted Ruby, full stop, with no curated allow-list of tags/filters standing between the template and the language. That puts ERB closer in spirit to Blade's @php escape hatch and to Next.js's own JSX (real JavaScript/TypeScript by default) than to Django — with one honest nuance: Blade normally chooses not to use raw PHP, reaching for its own sugared directives (@if, @foreach) that compile down to the same plain PHP @php would let through directly, while ERB has no separate sugared layer at all — <% %> already is the whole language, by design, not an escape hatch from something more restricted.
| Next.js (JSX) | Django (DTL) | Laravel (Blade) | Rails (ERB) | |
|---|---|---|---|---|
| Full host-language access | Yes — JSX is real JS/TS | No — a deliberately restricted separate language | Yes, via @php — though sugared directives are the default style | Yes — no separate language exists at all |
| Is there a "restricted mode"? | No | Yes — the entire point of DTL | No — @php is always available | No |
Hands-On Exercises
Explain what a Rails Helper module is, and why it's a genuinely different architectural option from both Django's "build it in the view" approach and Laravel's "build it in the Controller by convention" approach.
📄 View solutionExplain the real difference between ERB's own permissiveness and Blade's @php permissiveness, given that both technically allow full host-language code.
Explain why breadcrumb_for is expected to have a performance problem, and where in this course that problem is deliberately meant to be caught and fixed.
Chapter 4 Quick Reference
<%= yield %>— the layout's own insertion point, Rails' equivalent of@yield/{% block %}<%= %>/<% %>/<%== %>— output, execute-only, and unescaped outputapp/helpers/— a genuine third architectural layer for view-supporting logic, distinct from both siblings' own approaches- ERB's own permissiveness — no separate template language exists;
<% %>already is unrestricted Ruby, by design - Deliberate setup —
breadcrumb_for's N+1 pattern is left in place on purpose, caught in Chapter 6 - Next chapter: Styling — Dark Theme