Stimulus for Rails
Stimulus for Rails makes it easy to use this modest framework with the asset pipeline and ES6/ESM in the browser. It relies on either
importmap-rails to make Stimulus available via ESM or a node-capable Rails (like via
jsbundling-rails) to include Stimulus in the bundle. Make sure to install one of these first!
- Add the
stimulus-railsgem to your Gemfile:
With an import-mapped application, controllers are automatically pinned and registered based on the file structure. With a node application, controllers need to be imported and registered directly in the index.js file, but this is done automatically using either the Stimulus generator (
./bin/rails generate stimulus [controller]) or the dedicated
stimulus:manifest:update task. Either will overwrite the
You're encouraged to use the generator to add new controllers like so:
And it'll be activated and registered automatically when encountering the data-controller attribute in your DOM:
<div data-controller="hello"> <input data-hello-target="name" type="text"> <button data-action="click->hello#greet"> Greet </button> <span data-hello-target="output"> </span> </div>
Stimulus for Rails is released under the MIT License.