Understanding Rails

Understanding Rails · Season 1: The trip of one click

Hotwire

Episode 7 · 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: Added the route the edit link needs, which the example left out.

Turbo Drive, Streams, Frames, broadcasts, and Stimulus.

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

Transcript

0:00 Hotwire

Part seven. Hotwire.

Hotwire is how Rails makes pages feel instant, without writing much JavaScript. The idea is simple: send HTML over the wire, not JSON. It comes in two main pieces: Turbo, and Stimulus.

0:21 Turbo Drive

Turbo Drive watches every link and form. Instead of letting the browser load a whole new page, it fetches the page itself and swaps in the new body. No white flash, and no reloading every script and style.

That's what happened to our form. Turbo sent it with fetch, and added that accept header, asking for a turbo stream.

0:44 The stream lands

And here's the final moment of our click. The turbo stream arrives. Turbo reads the action, prepend. It finds the element with the ID notes. And it puts the new note at the top.

0:57 Stream actions

Prepend is one of a handful of actions: append, replace, update, remove, and more. Each one is a small, exact change to the page.

1:07 Turbo Frames

Turbo Frames do the same for navigation. Wrap part of a page in a frame, and links inside it replace only that part. Edit a note, and just that note becomes a form.

1:19 Broadcasts

Streams can travel over a WebSocket, too. One line in the model broadcasts each new note to everyone looking at Eagle Ridge. Their pages update live, without anyone clicking.

1:32 Stimulus

And when you do need JavaScript, Stimulus keeps it small. A controller is a class, attached with a data attribute. This one clears the note box once Turbo has submitted it. That's why the box was empty in the cold open.

That's the whole trip. Next: what Rails 8 gives you to run all of this in production.

Check your understanding

  1. This arrives: <turbo-stream action="prepend" target="notes">, with the new note inside a template. What does Turbo do?

    • Replaces the whole page with the note
    • Puts the note at the start of the element with the ID notes
    • Puts the note at the end of the element with the ID notes
    • Replaces the element with the ID notes
    Show answer

    Puts the note at the start of the element with the ID notes. prepend adds the template's content to the start of the target element. append would add it to the end. Source: Prepend, Streams (Turbo Reference).

  2. Turbo submits the note form with POST. How does the server know Turbo can take a turbo stream back?

    • Turbo adds a hidden field named turbo
    • The path ends in .turbo_stream
    • Turbo adds text/vnd.turbo-stream.html to the Accept header
    • The server always sends turbo streams to POST requests
    Show answer

    Turbo adds text/vnd.turbo-stream.html to the Accept header. For forms that POST, PATCH, PUT or DELETE, Turbo adds the turbo stream type to the Accept header. The controller's respond_to reads it. Source: Streaming From HTTP Responses, Come Alive with Turbo Streams (Turbo Handbook).

  3. A link sits inside a turbo-frame. You click it. What changes on the page?

    • Only the content of that frame
    • The whole body
    • Nothing until you reload
    • Every frame on the page
    Show answer

    Only the content of that frame. Links and forms inside a frame are captured, and only that frame's content is replaced from the response. Source: Decompose with Turbo Frames (Turbo Handbook).

  4. A form has data-controller="note-form". What does Stimulus do when it sees it?

    • Turns the form into a turbo-frame
    • Sends the form with fetch
    • Nothing until you call it from a script tag
    • Makes an instance of the controller in note_form_controller.js for that element
    Show answer

    Makes an instance of the controller in note_form_controller.js for that element. Stimulus reads the identifier from data-controller and creates an instance of the matching controller class. Source: Identifiers, Controllers (Stimulus Reference).

Sources

Comments

Sign in to comment or sign up.