Build interactive UIs with Hotwire and Stimulus
✓Works with OpenClaudeYou are a Rails developer building interactive UIs. The user wants to create dynamic, real-time components using Hotwire (Turbo + Stimulus) without writing JavaScript frameworks.
What to check first
- Run
rails -vto confirm Rails 6.1+ (Hotwire support included by default) - Check
Gemfile— ensurehotwire-railsis present (bundle list | grep hotwire) - Verify
app/javascript/controllersdirectory exists (Stimulus controller location)
Steps
- Generate a Stimulus controller with
rails generate stimulus controller_name— this createsapp/javascript/controllers/controller_name_controller.js - Define the controller class extending
Controllerand add aconnect()lifecycle hook that runs when the element enters the DOM - Create public methods in the controller — these become
data-actiontargets (e.g.,toggleMenu()called viadata-action="click->controller#toggleMenu") - Use
data-target="controller.elementName"on HTML elements to reference them in the controller asthis.elementNameTarget - Import and nest the Stimulus controller in your Rails view with
<div data-controller="controller-name"> - Add
data-action="event->controller#method"bindings to trigger controller methods (e.g.,data-action="click->counter#increment") - Use Turbo frames (
<turbo-frame id="unique-id">) to wrap content that should update without full page reload vialink_to, and respond with Turbo Stream responses - For form submissions, add
data-action="submit->form#submit"to forms and handle withevent.preventDefault()to use Fetch API with Railsrespond_to :turbo_stream
Code
// app/javascript/controllers/counter_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
// Define targets and values
static targets = ["count", "output"]
static values = { step: Number }
connect() {
// Runs when element with data-controller="counter" enters DOM
console.log("Counter controller connected")
this.currentCount = 0
}
increment(event) {
event.preventDefault()
this.currentCount += this.stepValue || 1
this.updateDisplay()
}
decrement(event) {
event.preventDefault()
this.currentCount -= this.stepValue || 1
this.updateDisplay()
}
reset(event) {
event.preventDefault()
this.currentCount = 0
this.updateDisplay()
}
updateDisplay() {
// Access targets: this.countTarget, this.outputTarget
this.outputTarget.textContent = `Count: ${this.currentCount}`
this.countTarget.value = this.currentCount
}
disconnect() {
//
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Ruby on Rails Skills
Other Claude Code skills in the same category — free to download.
Rails Setup
Scaffold Ruby on Rails app with models and controllers
Rails Active Record
Write Active Record models with associations and validations
Rails API
Build Rails API-only application with serializers
Rails Testing
Write RSpec tests with factories and mocks
Rails Sidekiq
Set up background jobs with Sidekiq
Rails ActiveRecord Performance Optimization
Fix N+1 queries, slow scopes, and ActiveRecord pitfalls in production Rails apps
Rails Action Cable WebSockets
Build real-time features in Rails using Action Cable WebSockets
Want a Ruby on Rails skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.