Rails, javascript not loading after clicking through link_to helper Rails, javascript not loading after clicking through link_to helper javascript javascript

Rails, javascript not loading after clicking through link_to helper


Are you using Rails 4? (Find out by doing rails -v in your console)

This issue is probably due to the newly added Turbolinks gem. It makes your application behave like a single page JavaScript application. It has a few benefits (it's faster), but it unfortunately breaks some existing events like $(document).ready() because the page is never reloaded. That would explain why the JavaScript works when you directly load the URL, but not when you navigate to it through a link_to.

Here's a RailsCast about Turbolinks.

There are a couple solutions. You can use jquery.turbolinks as a drop-in fix, or you can switch your $(document).ready() statement to instead use the Turbolinks 'page:change' event:

$(document).on('page:change', function() {    // your stuff here});

Alternatively, you could do something like this for compatibility with regular page loads as well as Turbolinks:

var ready = function() {    // do stuff here.};$(document).ready(ready);$(document).on('page:change', ready);

If you are using Ruby on Rails >5 (you can check by running rails -v in the console) use 'turbolinks:load' instead of 'page:change'

$(document).on('turbolinks:load', ready); 


I got the same probleme but jquery.turbolinks doesn't helped. I noticed that I have to override the GET method.

There is my sample :

<%= link_to 'Edit', edit_interpreter_path(@interpreter), method: :get %>


If you are on rails 5 instead of 'page:change' you should use 'turbolinks:load' like this:

$(document).on('turbolinks:load', function() {  // Should be called at each visit})

Source: https://stackoverflow.com/a/36110790