Stimulus controllers not working at first use, Rails 7

My current rails 7 app has been in development for a couple of months now. Still a novice, this is my first attempt to make use of a Stimulus controller.

Problem: Regardless of its content no controller I create / generate in this app will work.

About the application: Rails + Esbuild + Tailwind


// greetings_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  connect() {
    console.log("Hello, Stimulus!", this.element)

// index.html.erb
<div data-controller="greetings"></div>

None basically. No output in the console
(The controller was generated with: ./bin/rails generate stimulus controllerName)

What I know:

  1. Hotwire works, I have a pagination function with Turbo Stream based on:

  2. Something about Stimulus works, I use a scroll-to function, which I separately installed
    (I actually don’t know if this installation was necessary because stimulus’ integration in Rails 7, but I simply followed the guide)

  3. Strange behavior hello controller: the output is always the same regardless of its content. Meaning it will always output: “Hello World!” on the page, like in the original hello controller.
    This is even the case after deleting the controller, regenerating it and using the content of the the greetings controller above (or any other for that matter).

  4. Implementing the above controller content in the hello controller of an another Rails 7 test app works right away, and shows “Hello, Stimulus!” in the console, as expected. (this app runs on Rails, the content of /app/javascript/index.js looks different, don’t know if this matters)

To me it seems the code in the controllers isn’t used when the page is life, but I have no idea how to go about this.

All I can think of is reinstalling Stimulus, all though I don’t know how as it came with rails, this guess is as good as checking ‘if the cable is plugged in’.

Any suggestion is welcome.

This sounds like a problem with the assets being compiled and then esbuild not being able to do its own bundling I feel like I had the problem before and you can fix by
rails assets:clobber # Remove compiled assets
then try to do your bundling process again with bin/dev or whatever
You can also look in the rails logs for errors while the assets are compiling


My hero, you are spot on!

Thanks a lot

1 Like

Holy cow, man, I was looking badly for this solution.

Thank you so much! I love you hahahahah

Thanks very much, I was spending hours trying to figure out what was wrong and this finally helped!