Stimulus simply does not work in Rails 7.1.3

I’ve created a very plain vanilla Rails app. I’m at the stage where I need some Stimulus controllers, and I can’t make them initialize no matter what. Even the hello_controller doesn’t load. I’m creating a very simple controller using the generator. Despite reading that I don’t need to run the update manifest task, controllers don’t get pinned or imported automatically. Even after doing the pin/update song and dance, I can’t even get the connect method of the controller to log to the console.

What is the current, authoritative, state of the art set of directions about using Stimulus?

At this stage, I’m getting an error about mime-types regarding stimulus-loading. I don’t know. I’m starting to give up. I’m still on importmaps-rails, and while I’d love to just move to esbuild I can’t imagine the headache of doing that. As an aside, the current state of this part of Rails is incredibly disheartening. I’m sort of at my wit’s end.

Can you share a github repo?

It would be tough. There are some API keys and other things in it that I’m trying to not have to revoke/remake. If you think that’s the way I can look into. I realize it’s like flying blind without it, though.

Can you create a separate basic hello-world with Rails 7.1.3 that illustrates the problem?

Nick, I’m not sure if this is helpful, but I can confirm that I’m on Rails 7.1.3 and I’ve had no trouble with Stimulus. I’m also using importmap-rails and tailwindcss-rails to as to avoid requiring node.

Stimulus just worked for me, right out of the box, with this standard setup. And I found the documentation for Stimulus over (https://stimulus.hotwired.dev/) to be excellent. I’m new to stimulus but I’ve been feeling just how rock solid it is, both in terms of the integration into my app and the documentation to figure things out. At every turn I think of something I need, check the docs, there is a straightforward way to do it.

Probably there is something unexpected about your setup which is causing a conflict. If you are early enough, the easiest way to figure it out is to generate a brand new rails project, confirm Stimulus works, and copy over what you need from your existing project which has some broken config. That way you don’t actually need to get to the bottom of it but you’ll get things working.

Thanks for this response. I did exactly this and made a new app with rails new test app -c tailwind. I created a little controller:

import { Controller } from “@hotwired/stimulus”

// Connects to data-controller=“modal”
export default class extends Controller {
static targets = [“modal”]

close() {
alert(“Hello from modal_controller.js”)
this.modalTarget.remove()
}

}

and I found that while Rails does load the controller, it still will not run any function inside the controller. (All the other necessary parts were also created, so that was not the issue. Ther code is working code from another app, so that isn’t it.) In other words, a link like <%= link_to ‘Cancel’, ‘data-action’: ‘click->modal#close’ %> doesn’t log anything to the console. What are the chances that Tailwind’s installation is borking Stimulus somehow? Also, I’m running bin/dev for my server. Is that the issue? Completely mystified here.

Have you included data-controller="modal" anywhere? You’d probably want to define this on some parent element above where you have data-action="click->modal#close", like on a div wrapping the link_to.

1 Like

Yep same for me. In the end I had to ditch importmaps and move to esbuild and it worked straight away. See this video: https://youtu.be/Aw03k1X4zjA?si=GwCWxka_vVMl5-46

And this:

Sounds like you are hard-coding API keys in your app: any reason why you are doing this?

Re: reproducible app - where is the github link? The problem could be as simple as mis-specifying the data-controller (or mis-spelling it) or some such slight issue. Someone will surely be able to point you in the right direction.