Hotwire and Turbo
Hotwire is the argument that you do not need a JSON API and a client-side framework to build an interactive application. Send HTML over the wire and let the browser swap fragments. Turbo supplies the transport and the DOM operations; Stimulus supplies the small amount of behaviour that genuinely belongs in JavaScript. The name is an umbrella: Turbo (Drive, Frames, Streams) plus Stimulus.
The 60-second answer
Section titled “The 60-second answer”“Turbo Drive intercepts link clicks and form submissions, fetches the next page, and swaps <body> while keeping the same JavaScript context — fast navigation, still server-rendered HTML. Turbo Frames wrap a region in <turbo-frame id="...">; links and forms inside it fetch only that frame’s content and replace just that region, which is how inline editing and modals work with no custom JS. Turbo Streams are <turbo-stream action="append" target="..."> elements sent in a response or over a WebSocket, naming a target id and an action — append, prepend, replace, update, remove. In a model you broadcast with broadcast_replace_to/broadcast_append_to from Turbo::Broadcastable, and in a view turbo_stream_from @record subscribes the page. Stimulus controllers are imported from @hotwired/stimulus and attached with data-controller.”
Turbo Drive
Section titled “Turbo Drive”Drive is on by default: intercept the click, fetch the page, swap <body>, merge <head>, fire turbo:load. Two consequences to handle:
DOMContentLoadeddoes not fire again on navigation. Listen forturbo:load.- State in
<body>is discarded. Put it in the URL, a cookie, or a<head>element markeddata-turbo-track="reload".
Turbo Frames
Section titled “Turbo Frames”<%= turbo_frame_tag dom_id(@post, :edit) do %> <h1><%= @post.title %></h1> <%= link_to "Edit", edit_post_path(@post) %><% end %><%= turbo_frame_tag dom_id(@post, :edit) do %> <%= form_with model: @post do |f| %> <%= f.text_field :title %> <%= f.submit "Save" %> <% end %><% end %>The link inside the frame fetches the edit page; Turbo extracts the <turbo-frame> with the same id and swaps only that. No matching frame raises “frame missing” — the most common Turbo bug, almost always a mismatched dom_id.
data-turbo-frame="_top"breaks out of the frame and navigates the whole page.<turbo-frame id="..." src="/posts/42">lazy-loads on render.
Turbo Streams
Section titled “Turbo Streams”<%= turbo_stream.prepend "posts", partial: "posts/post", locals: { post: @post } %><%= turbo_stream.update "post_count", Post.count %><%= turbo_stream.replace "new_post" do %> <%= render "form", post: Post.new %><% end %>Actions: append, prepend, replace, update, remove, before, after. replace swaps the whole element including its id; update swaps only its children.
The template is chosen by the request format: Turbo sends Accept: text/vnd.turbo-stream.html, so respond_to takes the format.turbo_stream branch, while a plain form post gets format.html.
Live updates from models
Section titled “Live updates from models”<%= turbo_stream_from @room %>
<div id="messages"> <%= render @room.messages %></div>turbo_stream_from @room renders a <turbo-cable-stream-source> element that subscribes to the stream name Turbo derives for that record. Then broadcast from the model:
class Message < ApplicationRecord belongs_to :room
after_create_commit -> { broadcast_append_to room, target: "messages" } after_update_commit -> { broadcast_replace_to room } after_destroy_commit -> { broadcast_remove_to room }endbroadcast_*_to come from Turbo::Broadcastable, which turbo-rails mixes into ActiveRecord::Base. Broadcasts render the model’s partial (app/views/messages/_message.html.erb) unless you pass partial:.
Stimulus
Section titled “Stimulus”import { Controller } from "@hotwired/stimulus"
export default class extends Controller { static targets = ["source"]
async copy() { await navigator.clipboard.writeText(this.sourceTarget.value) this.element.dataset.copied = "true" setTimeout(() => delete this.element.dataset.copied, 2000) }}<div data-controller="clipboard"> <input data-clipboard-target="source" value="text"> <button data-action="clipboard#copy">Copy</button></div>- The element with
data-controlleristhis.element. data-action="click->clipboard#copy", or"clipboard#copy"for the default event.static targetsgeneratethis.sourceTargetandthis.hasSourceTarget.static valuesmapdata-*-valueattributes ontothis.fooValue.- Controllers are auto-registered from
app/javascript/controllers/index.jsby@hotwired/stimulus-loading; with importmaps there is no build step.
| Need | Tool |
|---|---|
| Faster navigation, no JS changes | Turbo Drive (default) |
| Replace a region from a link or form | Turbo Frame |
| Update several regions, or push from another user | Turbo Stream |
| Toggle, focus, drag, clipboard, third-party widget | Stimulus |
If you find yourself rendering JSON and building DOM in a Stimulus controller, you have rebuilt a SPA by accident.
Follow-up questions
Section titled “Follow-up questions”- Why does
DOMContentLoadedfire only once under Turbo Drive, and what replaces it? - What causes a “frame missing” error?
- Why must a failed form render with a 4xx status under Turbo?
- Why
after_create_commitrather thanafter_createfor broadcasts? - How does a broadcast reach a user connected to another process? (See Active Job and Action Cable.)
Common traps
Section titled “Common traps”import { Controller } from "stimulus". Wrong package; it is@hotwired/stimulus.- Broadcasting inside a transaction. Subscribers see rows that may be rolled back.
Turbo::StreamsChannel.broadcast_replace_towith no content. The channel method needs a renderable orhtml:; the model method renders the partial for you.turbo_stream_fromon an unsaved record. There is no id to derive a stream name from.- Missing
idon the target element. Streams address elements by id;dom_idkeeps them in sync.