Hey guys,
Im using the rails pipeline with not webpack
I have a stimulus controller
I have gem ‘hotwire-rails’ on my Gemfile
All is working well, except when I trying call Turbo.visit
on stimulus controller.
I saw here the js is exporting the Turbo
object, but different from ‘stimulus’ its not possible to import stuffs from ‘turbo’
import { Controller } from “stimulus” // Works
import { Turbo } from ‘turbo’ // not works
any ideia how access Turbo on assets pipeline?
Thanks
I’m having a similar problem except I am using webpacker.
if using webpacker
does not import { Turbo } from "@hotwired/turbo-rails"
works as expected?
I just added import "@hotwired/turbo-rails"
to my application.js
as per the install script:
# Some Rails versions use commonJS(require) others use ESM(import). TURBOLINKS_REGEX = /(import .* from "turbolinks".*\n|require\("turbolinks"\).*\n)/.freeze abort "❌ Webpacker not found. Exiting." unless defined?(Webpacker::Engine) say "Install Turbo" run "yarn add @hotwired/turbo-rails" insert_into_file "#{Webpacker.config.source_entry_path}/application.js", "import \"@hotwired/turbo-rails\"\n", before: /import.*ActiveStorage/ say "Remove Turbolinks" gsub_file 'Gemfile', /gem 'turbolinks'.*/, '' run "bin/bundle", capture: true run "bin/yarn remove turbolinks" gsub_file "#{Webpacker.config.source_entry_path}/application.js", TURBOLINKS_REGEX, '' gsub_file "#{Webpacker.config.source_entry_path}/application.js", /Turbolinks.start.*\n/, '' say "Enable redis in bundle" uncomment_lines "Gemfile", %(gem 'redis')
And am also now using import { Turbo } from "@hotwired/turbo-rails"
in my stimulus controller as per the readme.
It does seem to work but I get the “Turbo is not defined error” if I try to step through my code.
just tu check, are you using stimulus controller inside webpack ecosystem right (and not on app/assets/javascript
)?
Yes, using webpack. My stimulus controllers are in app/javascript/controllers
its pretty weird then
I will try to move it to webpack and check