Hotwire's Turbo and Stimulus
CREATED: 2026 / 05 / 05 Tue
UPDATED: 2026 / 05 / 05 Tue
What is Hotwire?
Hotwire is a set of tools built into Rails 7 by default that lets you build rich UIs with minimal JavaScript. It consists of the following components:
Turbo Drive → Speeds up page navigation (automatic, no code required)
Turbo Frame → Updates a portion of a page via GET requests
Turbo Stream → Updates multiple locations on POST/PUT/PATCH/DELETE
Stimulus → Adds interactivity to the screen with JavaScript while using Turbo
That said, these components themselves are written in JavaScript.
Turbo Drive
How It Works
Turbo Drive speeds up page navigation and is enabled automatically just by adding Turbo — no special code required.
Traditional Rails applications requested the entire page and reloaded all HTML, CSS, and JS. With Turbo Drive, the behavior changes as follows:
# Standard browser (without Drive)
Link click
→ Request entire page
→ Reload all HTML, CSS, JS
→ Re-render entire page
# With Turbo Drive
Link click
→ Request entire page
→ Replace only <body>
→ Do not reload CSS/JS in <head> ← This is the fast part
The more CSS and JS your app has, the greater the benefit from Turbo Drive.
The first render is the same as a normal browser, but the effect kicks in from the second page navigation onward.
Note: if JavaScript is disabled in the browser, it falls back to normal page navigation.
Turbo Frame
Basic Usage
Turbo Frame is a mechanism for updating only a portion of a page.
You can replace the HTML fetched via a GET request (from a Rails controller’s show or edit action) with the corresponding turbo-frame component.
You define a frame using the turbo_frame_tag helper:
<%# Frame on the page %>
<%= turbo_frame_tag "new_item" do %>
<%= render "form", item: @item %>
<% end %>
This generates the following HTML:
<turbo-frame id="new_item"> ... </turbo-frame>
When you call an API that returns this HTML, the browser extracts only the turbo-frame whose id matches the one in the response and performs a partial update — other frames are unaffected.
The server just returns a normal page; Turbo on the browser side handles the replacement.
Generating Unique IDs with dom_id
When displaying multiple records in a list, use the dom_id helper to generate a unique ID per record:
dom_id(item) # if item.id is 1 → "item_1"
# if item.id is 2 → "item_2"
<%= turbo_frame_tag dom_id(item) do %>
<turbo-frame id="item_1">...</turbo-frame> <turbo-frame id="item_2">...</turbo-frame>
This is how the id matching works.
Turbo Stream
Difference from Turbo Frame
| Turbo Frame | Turbo Stream | |
|---|---|---|
| Request | GET | POST/PUT/PATCH/DELETE |
| Update | Single frame | Multiple locations |
Turbo Frame is used to partially replace HTML fetched via GET requests, while Turbo Stream is used for screen updates triggered upon completion of POST/PUT/PATCH/DELETE requests — primarily for updating multiple locations at once.
7 Actions
Turbo Stream provides 7 methods for manipulating the DOM:
| Method | Behavior |
|---|---|
prepend | Add to the beginning of an element |
append | Add to the end of an element |
before | Insert before an element |
after | Insert after an element |
replace | Replace the entire element |
update | Replace only the inner content |
remove | Delete an element |
Usage Example
In create.turbo_stream.erb, you can group multiple screen update operations:
<%# create.turbo_stream.erb %>
<%= turbo_stream.prepend "items-list", partial: "items/item", locals: { item: @item } %>
<%= turbo_stream.replace "new_item" do %>
<%= turbo_frame_tag "new_item" do %>
<%= render "form", item: Item.new %>
<% end %>
<% end %>
turbo_stream.prepend adds the new item to the top of the list, and turbo_stream.replace resets the form to an empty state.
This is the strength of Turbo Stream — multiple locations can be updated in a single response.
Using respond_to
In the controller, use a respond_to block to differentiate between Turbo Stream responses and normal HTML responses:
respond_to do |format|
format.turbo_stream # selected when Accept: text/vnd.turbo-stream.html
format.html { redirect_to items_path } # selected when Accept: text/html
end
Requests sent by Turbo automatically include the Accept: text/vnd.turbo-stream.html header.
This causes format.turbo_stream to be selected, and the corresponding template file (e.g., create.turbo_stream.erb) is resolved automatically.
# Without block → auto-resolves create.turbo_stream.erb
format.turbo_stream
# With block → build Turbo Stream instructions inline
format.turbo_stream do
render turbo_stream: turbo_stream.replace("new_item") { ... }
end
When JavaScript is disabled in the browser, the Accept header is not sent automatically.
In that case, format.html is selected and the page is returned to the client in the traditional way.
Stimulus
Stimulus lets you use JavaScript (defined in controllers) nicely alongside Turbo. Combined with Turbo, it enables interactive UIs.
3 Core Concepts
data-controller → defines the scope of a controller
data-action → binds an event to a method
data-target → references a DOM element
data-value → passes data
Controllers
JavaScript only runs within the element marked with data-controller and its descendants.
<span data-controller="checkbox">
← scope starts <input data-[controller-name]-target="input" /> ← inside scope
</span>
← scope ends
<span class="item-title">...</span> ← outside scope, unrelated
Targets
Use data-[controller-name]-target to reference a DOM element:
static targets = ['input']
data-[controller-name]-target="input"
this.inputTarget // → references the <input> element itself
Values
Use data-[controller-name]-[value-name]-value to pass data:
static values = { url: String }
data-[controller-name]-url-value="/items/1"
this.urlValue // → "/items/1"
Actions
Use data-action to bind an event to a method:
data-action="[event-name]->[controller-name]#[method-name]"
data-action="change->checkbox#toggle"
Common events:
| Event | When it fires |
|---|---|
click | When clicked |
change | When a value changes |
submit | When a form is submitted |
keyup | When a key is released |
mouseover | When the mouse hovers over |
Auto-loading Controllers
Just follow the *_controller.js naming convention and controllers are loaded automatically with no configuration needed:
app/javascript/controllers/checkbox_controller.js
→ auto-registered as data-controller="checkbox"
The loading flow is as follows:
application.html.erb
└─ javascript_importmap_tags
└─ application.js
└─ import "controllers" (index.js)
└─ eagerLoadControllersFrom("controllers", application)
└─ auto-detects and registers checkbox_controller.js
In Closing
References:
- The Turbo Rails Tutorial | hotrails.dev
- Turbo — The speed of a single-page web application without having to write any JavaScript
- Introduction — Turbo Handbook
- Stimulus: A modest JavaScript framework for the HTML you already have
- The Origin of Stimulus — Stimulus Handbook
- Introduction — Stimulus Handbook