Search code examples
ruby-on-railsleafletasset-pipelinestimulusjshotwire-rails

Stimulus controllers not getting loaded


I am updated a Rails 6 app to Rails 7 and using importmaps and Leaflet. I am following How do I use Leaflet in Rails 7?. I successfully build a new app and it works fine, but upgrading my old app does not.

What am I missing? Or what debugging steps to try?

No errors in Terminal running server and no Console errors in Chrome. However in the new app following the answer, I see

Chrome dev mode sources. But in the updated app,

Upgraded app sources

Looks to me like the controllers are not getting loaded.

// app/javascript/application.js
import "@hotwired/turbo-rails"
import "controllers"
import "popper"
import "bootstrap"

# config/importmaps.rb
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true

pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"

pin "jquery", to: "https://ga.jspm.io/npm:[email protected]/dist/jquery.js", preload: true
pin "popper", to: 'popper.js', preload: true
pin "bootstrap", to: 'bootstrap.min.js', preload: true
pin "leaflet", to: "https://ga.jspm.io/npm:[email protected]/dist/leaflet-src.js", preload: true
pin "leaflet-css", to: "https://ga.jspm.io/npm:[email protected]/dist/leaflet.css.min.js", preload: true
pin "leaflet.control.opacity", to: "https://ga.jspm.io/npm:[email protected]/dist/L.Control.Opacity.js", preload: true
pin "leaflet.timeline", to: "https://ga.jspm.io/npm:[email protected]/dist/index.js", preload: true

// Gemfile
ruby '3.1.2' 
gem 'rails', '~> 7.0'
gem 'pg' , '~> 1.4.3'
gem "net-http" 
gem 'puma'
gem 'sassc-rails'
gem "importmap-rails", "~> 1.1"
gem "turbo-rails"
gem "stimulus-rails"
gem "sprockets-rails"
gem 'bootsnap', require: false
gem 'bootstrap', '~> 5.0.0'

// app/javascript/controllers/index.js
import { application } from "controllers/application"
import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
eagerLoadControllersFrom("controllers", application)

// app/javascript/application.js
import "@hotwired/turbo-rails"
import "controllers"
import "popper"
import "bootstrap"

Solution

  • Let me guess, you didn't update manifest.js. You should have app/javascript there:

    // app/assets/config/manifest.js
    
    //= link_tree ../images
    //= link_directory ../stylesheets .css
    //= link_tree ../../javascript .js
    //  ^ this one
    //= link_tree ../../../vendor/javascript .js