Search code examples
ruby-on-railsturbolinks

Turbolinks load event never firing in brand new Rails 5.1 app


For some reason Turbolinks is not firing the load event in my app, a brand new, recently created app.

I created a brand new Rails 5.1 app (5.1.4), using rails new (specifically rails new prueba -C -d postgresql --skip-coffee --no-api). This command automatically configures Rails to use Turbolinks, without the need of further manual configuration.

I then created a scaffold for a model (bin/rails g scaffold Thing name), just to have some pages to navigate to.

Finally, I created a file app/assets/javascripts/test.js with the following content:

document.addEventListener("turbolinks.load", function() {
  console.log("Turbolinks loaded")
});

After running the migration, I then run the server in development (bin/rails s), browse (tried both with Chrome v62 and Firefox v57), and check the developer tools console, but instead of seeing the "Turbolinks loaded" message, I get nothing:

  • When I visit /things, the console is empty
  • If I reload the same page, the console is still empty
  • If I follow the "New thing" link, the console is still empty
  • If, in the "New thing" page I follow the "Back" link, the console is still empty

According to the Turbolinks README:

[...] the turbolinks:load event, which fires once on the initial page load and again after every Turbolinks visit.

I was therefore expecting to see the message "Turbolinks loaded" in the console every single time.

Any ideas?


Solution

  • According to you code you mostly missed event name. You use turbolinks.load, when in fact it's turbolinks:load. Check this url for more details -- https://github.com/turbolinks/turbolinks#running-javascript-when-a-page-loads