Understanding Rails

Understanding Rails · Season 1: The trip of one click

Views

Episode 6 · overview · about 2 minutes · Rails 8.1 · by

How was this episode?

The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.

Was it clear?

Not endorsed yet. Are you a Rails developer? What's an endorsement?

Endorse this version To endorse: create an account, confirm your email, then link GitHub.

What changed

  • Version 2, November 20, 2026: Fixed the form so it shows the errors from a failed save.

ERB, form_with, partials, escaping, layouts, the turbo stream view.

Free to watch. If it helped, support the series.

Transcript

0:00 Views

Part six. Views.

A view turns data into HTML. Rails views are mostly HTML, with a little Ruby mixed in, in a format called E R B.

0:17 ERB

Two tags do most of the work. The first just runs Ruby. The second runs Ruby, and prints the result into the page.

0:25 form_with

Here's the trail page from the cold open. Its form is one helper, form with. It writes the form tag, the path for our notes, and that hidden authenticity token.

0:37 Partials

Each note is drawn by a partial, a small template whose name starts with an underscore. Dom ID gives every note its own ID, like note three seventeen.

The trail page draws the whole list with one line: render trail dot notes. Rails finds the partial on its own.

0:57 Escaping

And E R B protects you. If someone types a script tag into a note, it's printed as plain text, never run.

1:05 Layouts

Full pages are wrapped in a layout: the head, the navigation, everything shared. Yield marks where each page goes.

1:14 The turbo stream view

But our click didn't ask for a full page. For a turbo stream, Rails renders create dot turbo stream dot E R B. One line: prepend this note to the notes list. That's the answer we saw in part one.

That small piece of HTML travels back to the browser. What happens when it lands? That's Hotwire, in part seven.

Check your understanding

  1. Which line puts the note's body into the page?

    • <% note.body %>
    • <%= note.body %>
    • <%# note.body %>
    • <% puts note.body %>
    Show answer

    <%= note.body %>. <%= %> runs Ruby and prints the result into the page. <% %> runs Ruby but prints nothing. Source: ERB, Action View Overview (Rails Guides 8.1).

  2. What does dom_id(note) return for the note with ID 317?

    • "note_317"
    • "317"
    • "notes-317"
    • "#note_317"
    Show answer

    "note_317". dom_id uses the model's singular name, an underscore, then the ID. Source: ActionView::RecordIdentifier (Rails API 8.1).

  3. The trail page says <%= render @trail.notes %>. Which file draws each note?

    • app/views/notes/note.html.erb
    • app/views/trails/_notes.html.erb
    • app/views/notes/_note.html.erb
    • None: you have to name the partial
    Show answer

    app/views/notes/_note.html.erb. Rails looks at the model name, Note, and uses the partial named _note in the notes folder. Source: Rendering Collections, Action View Overview (Rails Guides 8.1).

  4. A hiker types <script>alert(1)</script> as a note, and the partial prints it with <%= %>. What happens in other hikers' browsers?

    • The script runs once
    • The note is left out of the page
    • Rails raises an error
    • The tags show as plain text, and nothing runs
    Show answer

    The tags show as plain text, and nothing runs. Rails escapes what <%= %> prints by default. You only skip that with raw or html_safe, which you should never do with user input. Source: ActionView::Helpers::OutputSafetyHelper (Rails API 8.1).

Sources

Comments

Sign in to comment or sign up.