###Turbolinks and Javascrit
You might have some weird behavior in your rails app if you use Turbolinks and then you write some Javascript code without considerig the way Turbolinks actually works.
If you expect your Javascript code to run inside of $(document).ready()
, you will notice that it doesn't always happen. Your code will run maybe the first time you load the page in the browser, but not the subsequent times. Maybe won't run also when you access a page by clicking on a link (but then it will work when you refresh your page).
Using Turbolnks, when you click on a link, your web browswer won't send a request to the server in order to get the content. Instead, it will replace the content of the with the parsed result of an ajax request to the link. This way, all the code that you wrote under
$(document).ready(function(){
// some more code
})
won't run because Turbolinks overrides the usual way the DOM is loaded. In order to make your code work, you can write somet like this:
$(document).on("page:change", function() {
alert("page has loaded!");
});
Then your Javascript code will run as expected. There's some links where you can read more about this:
Boris Staal Blog
Rails Guides
Turbolinks