Build a Simple Like Button with Stimulus and Turbo
📖 Scenario: You are creating a small interactive feature for a blog post page. Readers can click a like button to increase the like count without reloading the page.This uses Stimulus to handle the button click and Turbo to update the like count dynamically.
🎯 Goal: Build a like button that increments the like count on click using Stimulus controller and Turbo frame updates.
📋 What You'll Learn
Create a Turbo Frame with a unique ID to wrap the like count
Create a Stimulus controller named like to handle button clicks
Add a data-action to the button to call the Stimulus controller method
Update the like count inside the Turbo Frame without full page reload
💡 Why This Matters
🌍 Real World
This pattern is common in modern Rails apps to create fast, interactive UI elements without full page reloads, improving user experience.
💼 Career
Understanding Stimulus and Turbo is essential for Rails developers building responsive, real-time features efficiently.
Progress0 / 4 steps
1
Set up the Turbo Frame and initial like count
Create a Turbo Frame tag with id="like_count_frame" that contains a span showing the initial like count of 0.
Ruby on Rails
Hint
Use the <turbo-frame> tag with the exact id="like_count_frame" and put a <span> inside showing 0.
2
Add the Stimulus controller and button
Add a button with data-controller="like" and data-action="click->like#increment". The button text should be Like.
Ruby on Rails
Hint
Add a <button> with the exact data-controller="like" and data-action="click->like#increment" attributes and text 'Like'.
3
Create the Stimulus controller with increment method
Create a Stimulus controller named like_controller.js with an increment method that fetches the Turbo Frame with id="like_count_frame" and updates its inner span by increasing the number by 1.
Ruby on Rails
Hint
Inside increment(), get the element with id="like_count_frame", find the span, read its number, add 1, and update the text.
4
Connect Stimulus controller in HTML and finalize
Ensure the Stimulus controller is loaded by adding data-controller="like" to the button and that the Turbo Frame and button are inside the same HTML snippet. Confirm the button click updates the like count inside the Turbo Frame.
Ruby on Rails
Hint
Make sure the button has data-controller="like" and data-action="click->like#increment" and the Turbo Frame is present in the HTML.
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