0

My Jquery files are only loading when page is refreshed. But when I remove (import "@hotwired/turbo-rails") from application.js It works normally. But removing "@hotwired/turbo-rails" causing other issues. Anyone please help.

  • The question you asked is extremely general and without much more info it will likely be impossible to answer. To start, is it happening in development (locally)? Are there any errors in the log or browser console? – Mike Szyndel Aug 07 '22 at 18:52
  • I am having the same problem. All my link_to and redirects result in the page loading without my javascript working correctly. All the solutions I've found are for disabling turbo-links, so I'm guessing this issue is something to do with hotwire/turbo-rails, or how I'm structuring my JS, but I still haven't figured out what. I was going through this [link](https://dev.to/nejremeslnici/using-hotwire-turbo-in-rails-with-legacy-javascript-17g1) to see if I could find something that might apply to me/ help me. I'll let you know if I figure it out, good luck. – WantsomeChocolate Aug 12 '22 at 00:14

2 Answers2

1

Change

$(document).on('ready', function () { ... }

to

document.addEventListener('turbo:load', () => { ... }

David Harkness
  • 166
  • 1
  • 1
  • 7
0

Like Mike said it's hard to know your exact issue without more detail, but it sounds like an issue I was just having with my link_to and redirects not triggering my javascript events (so my DataTables and certain componants of the random html5 template I'm using were not working).

I was reading through this link and it mentioned making turbo-drive "opt-in" globally across the app by adding this code to the bottom of your app/javascript/controllers/application.js

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = false

Search for "Disabling Turbo by default" in the link to get to the section I'm referring to.

I'm sure there is a better solution that involves keeping turbo-drive enabled and writing better JS or putting it in a better location, but at least now the JS in my app/javascript/application.js file as well as the JS files in the app/assests/javascripts directory are working the way I expect.

Thanks.