Learn how to add interactive behavior to your Rails application with minimal JavaScript.
Author: J3
Reading Time: ~3 min
The best way to learn how Stimulus works is by building a simple app.
Stimulus automatically connects DOM elements to JavaScript controller objects.
Let’s Get Started!
_Note: If you get stuck, please see my page.
ruby -v
# ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-linux]
rails -v
# Rails 8.0.2
mkdir stimulus
cd stimulus/
rails new greetings_app
cd greetings_app/
code .
In Rails 8, Stimulus is integrated by default. No need to run:
rails turbo:install stimulus:install
✅ Default Stimulus Setup in Rails 8:
stimulus-rails gem included.
app/javascript/controllers/ directory created.
index.js auto-loads controllers.
application.js imports controllers.
Import Maps configured in config/importmap.rb.
Start using Stimulus immediately!
rails generate controller greetings index
Pin local-time
bin/importmap pin local-time
Update Configuration
config/application.rb
:
config.time_zone = "America/Porto_Velho"
config.active_record.default_timezone = :local
app/javascript/application.js
:
import LocalTime from "local-time"
LocalTime.start()
View:
app/views/greetings/index.html.erb
<h1>Welcome to Greetings App!</h1>
<strong>Your Local Time is:</strong>
<%= time_tag Time.now, "data-local": "time", "data-format": "%B %e, %Y %l:%M %P" %>
config/routes.rb
:
root "greetings#index"
rails generate stimulus greeting
Creates: app/javascript/controllers/greeting_controller.js
greeting_controller.js
:
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["name", "output"];
connect() {
console.log("GreetingController connected");
}
greet() {
const name = this.nameTarget.value.trim();
this.outputTarget.textContent = name
? `Hello, ${name}!`
: "Please enter your name.";
}
}
app/views/greetings/index.html.erb
:
<div data-controller="greeting">
<label for="name">Enter your name:</label>
<input id="name" data-greeting-target="name" type="text" placeholder="Your name">
<button data-action="click->greeting#greet">Greet Me</button>
<p data-greeting-target="output" style="margin-top: 20px;"></p>
</div>
bin/dev
✅ Conclusion
This is a simple app designed to introduce you to Rails 8 and Stimulus. With Stimulus and Import Maps, you can build interactive features while keeping your Rails app simple and server-rendered. 📚 References
Stimulus in Rails by Tejal Panjwani
Stimulus Starter Project by DHH (Basecamp, LLC)
👉 Ready to dive deeper? Check out the official Stimulus tutorial.