omza2w
Last Updated: February 25, 2016
·
442
· amirtcheva
Dd1ca1288610b1750af31b0f22a51d6c

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

Say Thanks
Respond