Search code examples
jqueryruby-on-railstwitter-bootstrapturbolinksruby-on-rails-5

link_to links not loading javascript in rails


In my index view, I have a table(which uses bootstrap table sorting javascript library to sort the columns) I am using rails 5.

So now when I click on the <%= link_to 'Products', products_path %>, the javascript is not getting loading but when I reload the browser(ctrl + r) I see the table sorting starts working. I can see this happening after watching the network tab in Chrome Developer, with link_to dont see any requests made to the js file but when I reload, the js file is available.

I am using standard rails structure of

  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

My guess is something is happening due to turbolinks, but I am unable to figure out


Solution

  • For Rails 5 (Turbolinks 5) page:load becomes turbolinks:load and will be even fired on initial load. so add your javascript code for bootstrap table sorting go inside following block.

    $(document).on('turbolinks:load', function() {
    
      ...your javascript goes here...
    
    });