Setting up Hotwire on Rails

I’ve had some troubles setting up Hotwire on a new Rails app, these are my findings.

Initial setup

Add to your gemfile

    gem 'hotwire-rails'

This includes turbo-rails and stimulus-rails and will not work if you don’t have sprockets. If you created your rails app with the --skip-sprokets options you will have to readd them.

Run the automatic setup:

    ./bin/rails hotwire:install

This will create a few files, enable the redis gem and add turbo-rails and stimulus to your package.json file.

Stimulus troubles

The first javascript errors show up:

    Uncaught Error: Cannot find module 'controllers'

It is discussed here. You can fix it by running

    ./bin/rails webpacker:install:stimulus

This will add the missing app/javascripts/controllers/index.js file. Once that is done, stimulus should be fine.

Turbo

If your application.js has

    import "@hotwired/turbo-rails"

Then you should be alright.