Last Updated: February 25, 2016
·
774
· amirtcheva

Working with Turbolinks and jQuery in Rails 4

I had a huge problem getting my JavaScript to fire on page reloads, and after many hours(days?) of trying to get this to work, I found the solution below:

var ready = function pageLoad() {

//(all your callback functions here)
$('.class').on('click', function(event) {
     .....
 })
}

$(document).ready(ready)
$(document).on('page:load', ready)

This worked for me (using Rails 4.1.0, Ruby 2.0.0). Make sure to use .on(), instead of the direct .click().