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 7.0.2.4 + Esbuild + Tailwind
Example:
// 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>
Result:
None basically. No output in the console
(The controller was generated with: ./bin/rails generate stimulus controllerName)
What I know:
-
Hotwire works, I have a pagination function with Turbo Stream based on:
colby.so/posts/pagination-and-infinite-scrolling-with-hotwire -
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) -
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). -
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 7.0.2.3, 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.