Stimulus for Rails
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!
This gem is automatically configured for applications made with Rails 7+ (unless
--skip-hotwire is passed to the generator). But if you're on Rails 6, you can install it manually with:
- Add the
stimulus-railsgem to your Gemfile:
Usage with import map
With an import-mapped application, controllers are automatically pinned and registered based on the file structure. The installer will amend your
config/importmap.rb to configure this such that all controllers in
By default, your application will be setup to eager load all the controllers mentioned in your import map under "controllers". This works well together with preloading in the import map when you have a modest number of controllers.
If you have a lot of controllers, you may well want to lazy load them instead. This can be done by changing from
lazyLoadControllersFrom in your
When lazy loading, controllers are not loaded until their data-controller identifier is encountered in the DOM.
./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:
Stimulus for Rails is released under the MIT License.