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 uses the 7kb es-module-shim to provide importmap support for all ES6-compatible browsers. This means you can develop and deploy without using any bundling or transpiling at all! Far less complexity, no waiting for compiling.
If you want to use Stimulus with a bundler, you should use Webpacker instead. This gem is purely intended for those who wish to use Stimulus with the asset pipeline using ESM in the browser.
- Add the
stimulus-railsgem to your Gemfile:
The last command will:
- Create an example controller in
- Append the include tags to the
- Initialize your
app/assets/config/manifest.jsfile for compilation.
With the Stimulus include tags added, you'll automatically have activated Stimulus through the controller autoloader. You can now easily add new Stimulus controllers that'll be loaded via ESM dynamic imports.
For example, a more advanced
hello_controller could look like this:
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>
You can add additional libraries needed by your controllers in
email@example.com naming convention. These libraries will be added to the dynamically generated importmap (a shim is included with the
stimulus_include_tags), so you can reference
import Cookie from "cookies".
The libraries must be made for ESM. See https://skypack.dev where you can either directly reference libraries or download them and use them with the ESM conversion.
Stimulus for Rails is released under the MIT License.