JQuery gets loaded only on page refresh in Rails 4 application

Okay, I think I understand your problem enough to provide an answer. The thing about using turbolinks is that most plugins and libraries that bind to the document ready event stop working, since turbolinks prevents the browser from reloading the page. There are tricks to fix those issues, but the easiest way to fix it is to use jquery.turbolinks.

To use it, just add this to your Gemfile:

gem 'jquery-turbolinks'

and this to your assets/javascripts/application.js file:

//= require jquery.turbolinks

and you should be good to go.

FYI: You don’t really need to use turbolinks, but it’s useful and it makes requests faster by avoiding a full page refresh. Turbolinks fetches the contents of the link you clicked via AJAX and renders it on the same page, thus eliminating the overhead of reloading assets (JS and CSS). Try to make your page work with it. Using the library in the previous paragraph I’ve had no real issues. The more CSS and JS you have on your page, the bigger the improvement you get by using turbolinks.

Leave a Comment