I am using Stimulus (via stimulus-rails 0.1.2) in a Rails (6.0.3.4) application.
I have an ajax call via fetch in a Stimulus controller (which works great). The results of the fetch add some elements to the dom. These elements contain data-controller and data-action attributes on the elements.
However, the action does not fire on the newly added DOM elements. Is there something that I need to do on the front end to get these new dom elements to be “hooked into” the Stimulus lifecycle?
AFAIK app/assets is used by the asset pipeline, so you probably want to use app/javascript/ instead, with app/javascript/controllers/index.js:
// Load all the controllers within this directory and all subdirectories.
// Controller files must be named *_controller.js.
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start()
const context = require.context("controllers", true, /_controller\.js$/)
application.load(definitionsFromContext(context))
Then your controller would go to app/javascript/controllers/addresses_controller.js. Does that solve your issue?
It seems like stimulus-rails will setup Stimulus to loaded by Asset Pipeline when running rails stimulus:install. I don’t think it is necessary to move everything into app/javascript/.
Has everything been setup properly as described in the installation section of the README? If so, it seems like everything should work “automagically”
With the Stimulus include tags added, you’ll automatically have activated Stimulus through the controller autoloader. You can now easily add new Stimulus controllers that’ll be loaded via ESM dynamic imports.
Do you see the console.log messages if the page renders an HTML element with data-controller='addresses' on initial load rather than asynchronously?