2

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

H D
  • 603
  • 1
  • 8
  • 26
  • 1
    Just tried the second answer from here - http://stackoverflow.com/questions/17600093/rails-javascript-not-loading-after-clicking-through-link-to-helper - it still works. So, basically `<%= link_to 'Products', products_path, method: :get %>` should do the trick too. – mohnstrudel Sep 10 '16 at 12:27

1 Answers1

5

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...

});
power
  • 1,225
  • 7
  • 16