Skip to content

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.

“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.”

Drive is on by default: intercept the click, fetch the page, swap <body>, merge <head>, fire turbo:load. Two consequences to handle:

  • DOMContentLoaded does not fire again on navigation. Listen for turbo:load.
  • State in <body> is discarded. Put it in the URL, a cookie, or a <head> element marked data-turbo-track="reload".
app/views/posts/show.html.erb
<%= turbo_frame_tag dom_id(@post, :edit) do %>
<h1><%= @post.title %></h1>
<%= link_to "Edit", edit_post_path(@post) %>
<% end %>
app/views/posts/edit.html.erb
<%= 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.
app/views/posts/create.turbo_stream.erb
<%= 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.

app/views/rooms/show.html.erb
<%= 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:

app/models/message.rb
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 }
end

broadcast_*_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:.

app/javascript/controllers/clipboard_controller.js
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-controller is this.element.
  • data-action="click->clipboard#copy", or "clipboard#copy" for the default event.
  • static targets generate this.sourceTarget and this.hasSourceTarget.
  • static values map data-*-value attributes onto this.fooValue.
  • Controllers are auto-registered from app/javascript/controllers/index.js by @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.

  1. Why does DOMContentLoaded fire only once under Turbo Drive, and what replaces it?
  2. What causes a “frame missing” error?
  3. Why must a failed form render with a 4xx status under Turbo?
  4. Why after_create_commit rather than after_create for broadcasts?
  5. How does a broadcast reach a user connected to another process? (See Active Job and Action Cable.)
  • 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_to with no content. The channel method needs a renderable or html:; the model method renders the partial for you.
  • turbo_stream_from on an unsaved record. There is no id to derive a stream name from.
  • Missing id on the target element. Streams address elements by id; dom_id keeps them in sync.