How do I install StimulusJS to a Ruby on Rails application?
The docs mention webpack
and npm
, but nothing about Rails or Yarn (at time of this writing).
(Kind of strange given that Stimulus comes from Basecamp, who literally created RoR...)
The answer, at least for Rails 6, is stunningly easy:
bundle exec rails webpacker:install:stimulus
That's it. That command will:
yarn
to download and install StimulusJSapp/javascript/packs/application.js
to import javascript controllersapp/javascript/controllers/index.js
(to load/init all stimulus controllers)app/javascript/controllers/hello_controller.js
From there, you can create a simple view to use the demo Hello controller, and with a little tweaking you can make it just like the demo on the StimulusJS.org home page.
Webpacker instructions for Stimulus are here: https://github.com/rails/webpacker/blob/5-x-stable/docs/integrations.md#stimulus