Grant Birchmeier
Grant Birchmeier

Reputation: 18484

How do I install StimulusJS to a Ruby on Rails application?

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...)

Upvotes: 3

Views: 625

Answers (1)

Grant Birchmeier
Grant Birchmeier

Reputation: 18484

The answer, at least for Rails 6, is stunningly easy:

bundle exec rails webpacker:install:stimulus

That's it. That command will:

  • invoke yarn to download and install StimulusJS
  • add a line to app/javascript/packs/application.js to import javascript controllers
  • add file app/javascript/controllers/index.js (to load/init all stimulus controllers)
  • add a demo controller in 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

Upvotes: 7

Related Questions