Stimulus is a lightweight JavaScript framework that adds behavior to HTML by connecting controllers to elements. It helps you write small, reusable JavaScript pieces that respond to user actions without heavy frameworks.
Click to reveal answer
beginner
What role does Turbo play in Hotwire?
Turbo speeds up web apps by replacing full page reloads with partial updates. It intercepts links and form submissions to update only parts of the page, making apps feel faster and smoother without writing much JavaScript.
Click to reveal answer
beginner
How do Stimulus controllers connect to HTML elements?
Stimulus controllers connect using data attributes like data-controller on HTML elements. This tells Stimulus which controller to use, and you can add actions with data-action to respond to events like clicks.
Click to reveal answer
intermediate
What is a Turbo Frame and how does it improve user experience?
A Turbo Frame is a part of the page that can update independently. When you click a link or submit a form inside a Turbo Frame, only that frame reloads, not the whole page. This makes interactions faster and smoother.
Click to reveal answer
intermediate
How do Turbo Streams help update the page dynamically?
Turbo Streams let the server send small HTML snippets that tell the browser how to update parts of the page (like adding or removing elements) without a full reload. This keeps the page in sync with server changes in real time.
Click to reveal answer
Which attribute connects a Stimulus controller to an HTML element?
Adata-turbo
Bdata-frame
Cdata-action
Ddata-controller
✗ Incorrect
The data-controller attribute tells Stimulus which controller to attach to the element.
What does Turbo primarily replace to make web apps faster?
AFull page reloads
BCSS stylesheets
CJavaScript event listeners
DDatabase queries
✗ Incorrect
Turbo intercepts links and form submissions to avoid full page reloads, updating only parts of the page.
Which Hotwire component allows partial page updates by targeting specific page sections?
AStimulus Controllers
BTurbo Frames
CWebpack
DAction Cable
✗ Incorrect
Turbo Frames let you update only parts of the page independently, improving speed and user experience.
How do Turbo Streams update the page?
ABy running JavaScript timers
BBy reloading the entire page
CBy sending HTML snippets with instructions to modify the DOM
DBy changing CSS styles
✗ Incorrect
Turbo Streams send small HTML fragments that tell the browser how to update parts of the page dynamically.
Which of these is NOT a feature of Stimulus?
AHandling server-side database queries
BConnecting controllers via data attributes
CResponding to user events
DAdding behavior to HTML elements
✗ Incorrect
Stimulus is a JavaScript framework for client-side behavior; it does not handle server-side database queries.
Explain how Stimulus and Turbo work together to improve a Rails web app's user experience.
Think about how small JavaScript pieces and fast page updates combine.
You got /4 concepts.
Describe how you would use a Stimulus controller to respond to a button click in a Rails app using Hotwire.
Focus on connecting HTML attributes to controller methods.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Stimulus in a Rails application using Hotwire?
easy
A. To add simple JavaScript behavior to HTML elements using controllers and data attributes
B. To replace the entire page content on every user action
C. To handle database queries efficiently
D. To style HTML elements with CSS
Solution
Step 1: Understand Stimulus role
Stimulus is designed to add JavaScript behavior to HTML by connecting controllers to elements via data attributes.
Step 2: Compare other options
Replacing entire page content is Turbo's job, not Stimulus. Database queries and styling are unrelated.
Final Answer:
To add simple JavaScript behavior to HTML elements using controllers and data attributes -> Option A
Quick Check:
Stimulus = JavaScript behavior via controllers [OK]
When a new message is created, the frame does not update automatically. What is the likely cause?
medium
A. The frame id should be messages_frame instead of messages
B. Turbo Frames cannot render collections
C. The render call is missing a partial
D. The server response does not include a turbo-frame with matching id
Solution
Step 1: Understand Turbo Frame update mechanism
Turbo Frames update only if the server response includes a turbo-frame element with the same id as the frame on the page.
Step 2: Identify the problem
If the server response lacks a matching turbo-frame wrapper, Turbo cannot replace the frame content automatically.
Final Answer:
The server response does not include a <turbo-frame> with matching id -> Option D
Quick Check:
Matching turbo-frame id in response is required [OK]
Hint: Ensure server response wraps content in matching turbo-frame id [OK]
Common Mistakes:
Changing frame id arbitrarily
Assuming Turbo Frames can't render collections
Forgetting to wrap response in turbo-frame tag
5. You want to create a live search feature using Turbo and Stimulus. Which approach correctly combines both to update search results as the user types without full page reload?
hard
A. Use Stimulus to reload the whole page manually on input change
B. Use Turbo Frames alone to reload the entire page on every keystroke
C. Use a Stimulus controller to listen for input events, send fetch requests to the server, and update a Turbo Frame with the results
D. Use Turbo Streams to replace the entire page content on input
Solution
Step 1: Understand live search requirements
Live search updates results dynamically as the user types without full reload.
Step 2: Combine Stimulus and Turbo correctly
Stimulus listens to input events and sends fetch requests. Turbo Frame updates only the results part of the page.
Step 3: Evaluate other options
Reloading entire page on every keystroke is inefficient. Turbo Streams replace parts but need server push. Stimulus alone can't reload page efficiently.
Final Answer:
Use a Stimulus controller to listen for input events, send fetch requests to the server, and update a Turbo Frame with the results -> Option C