0
0
Ruby on Railsframework~10 mins

Stimulus and Turbo (Hotwire) in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stimulus and Turbo (Hotwire)
User interacts with page
Turbo intercepts link/form
Turbo fetches HTML via AJAX
Turbo updates page content
Stimulus controller detects changes
Stimulus runs JS behavior
Page updated dynamically without reload
User actions trigger Turbo to fetch and update HTML dynamically, then Stimulus runs JavaScript behaviors on the updated content.
Execution Sample
Ruby on Rails
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  connect() {
    console.log("Hello from Stimulus!")
  }
}
This Stimulus controller logs a message when connected to the page element.
Execution Table
StepActionTurbo BehaviorStimulus BehaviorPage State
1User clicks a link with data-turboIntercepts click, prevents full reloadNo action yetPage stays, no reload
2Turbo fetches new HTML via AJAXLoads new HTML fragmentNo action yetPage content ready to update
3Turbo replaces target element with new HTMLUpdates DOM dynamicallyDetects new elementsPage content updated without reload
4Stimulus controller connects to new elementsNo actionRuns connect() method, logs messageJS behavior active on new content
5User submits form with data-turboIntercepts submit, sends AJAX requestNo action yetForm submission handled dynamically
6Turbo updates page with server responseReplaces part of page with new HTMLStimulus reconnects controllersPage updated, JS behaviors active
7User interacts with Stimulus-controlled elementNo Turbo actionStimulus handles event, runs methodsDynamic interaction without reload
8End of interactionNo further Turbo actionsStimulus remains activePage fully interactive dynamically
💡 User stops interacting or navigates away; Turbo and Stimulus keep page dynamic without full reloads.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
Page HTMLInitial full pageUpdated with Turbo HTML fragmentSame as Step 3Updated again with Turbo responseFinal updated page
Stimulus ControllersConnected to initial elementsDisconnected from replaced elementsConnected to new elementsReconnected after Turbo updateActive and ready
Console LogsEmptyEmpty"Hello from Stimulus!""Hello from Stimulus!" (again if reconnected)Logs show Stimulus connect calls
Key Moments - 3 Insights
Why does the page not fully reload when clicking a link with data-turbo?
Because Turbo intercepts the link click (see execution_table step 1) and fetches only the needed HTML via AJAX, updating the page dynamically without a full reload.
How does Stimulus know when to run JavaScript on new content?
Stimulus detects when new elements are added to the page by Turbo (step 4 and 6) and automatically connects controllers to those elements, running their connect() methods.
What happens to Stimulus controllers when Turbo replaces part of the page?
Stimulus disconnects controllers from removed elements and reconnects to new elements after Turbo updates the DOM (see variable_tracker and execution_table steps 3-6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AUser submits a form
BStimulus disconnects all controllers
CTurbo updates the page content dynamically without reload
DPage fully reloads
💡 Hint
Check the 'Turbo Behavior' and 'Page State' columns at step 3 in the execution_table.
At which step does Stimulus run its connect() method and log a message?
AStep 4
BStep 2
CStep 6
DStep 1
💡 Hint
Look at the 'Stimulus Behavior' column in the execution_table for when connect() runs.
If Turbo did not intercept the form submission at step 5, what would happen?
AStimulus would run connect() again
BThe page would reload fully on form submit
CTurbo would update the page dynamically
DNothing would happen
💡 Hint
Refer to the 'Turbo Behavior' column at step 5 and think about what happens without Turbo interception.
Concept Snapshot
Stimulus and Turbo (Hotwire) work together to make Rails pages dynamic.
Turbo intercepts links and forms to fetch and update HTML without full reloads.
Stimulus runs JavaScript controllers on page elements, connecting automatically after Turbo updates.
This combo creates fast, interactive pages with minimal JavaScript.
Use data-turbo attributes for Turbo and data-controller for Stimulus.
Stimulus connect() runs when elements appear in the DOM.
Full Transcript
This visual execution shows how Stimulus and Turbo (Hotwire) work together in Rails. When a user clicks a link or submits a form with data-turbo, Turbo intercepts the action and fetches new HTML via AJAX instead of reloading the whole page. Turbo then updates the page content dynamically. Stimulus detects these changes and connects its controllers to the new elements, running JavaScript behaviors like logging messages. The execution table traces each step from user interaction to page update and JavaScript activation. Variables like page HTML and Stimulus controllers change state as Turbo updates the DOM and Stimulus reconnects. Key moments clarify why the page does not reload fully, how Stimulus knows to run, and what happens to controllers during updates. The quiz tests understanding of Turbo's interception, Stimulus connection timing, and consequences of missing Turbo interception. The snapshot summarizes the synergy of Turbo and Stimulus for dynamic Rails pages with minimal reloads and JavaScript.