What is turbolinks ?
We know that, in Rails 4 gem
turbolinks is added by default.
This gem makes our Rails application appears faster to user by using
Check in Rails 4 app for turbolinks -
//= require turbolinks
When we use Turbolinks this events callback is only triggered on the initial page load.
There are several events that Turbolinks will trigger, one of which is called page:load.
ready = -> $('#show_alert').click -> alert "Alert- Turbolinks works here." $(document).ready(ready) $(document).on('page:load', ready)
we set the function on click event to a variable called ready and pass it to both the
document.ready and the
page:load events. This way the events for the #show_alert will be attached whether we’ve loaded the page via Turbolinks or not.
Other solution is, we can use
Add gem 'jquery-turbolinks' in Gemfile.
require it immediately after jquery.js. Your other scripts should be loaded after jquery.turbolinks.js, and turbolinks.js should be after your other scripts.
//= require jquery //= require jquery.turbolinks //= require jquery_ujs // // ... your other scripts here ... // //= require turbolinks
And it works!