what rails version you use?
if rails 4+, u need more document event listener like $(document).on('page:change', function(){})
because rails 4+ using turbolinks gem to make loading page faster, so your jQuery.ready() listeners only executed at first page load, not working in another page.
There are some events provided by turbolinks:
page:before-change
page:fetch
starting to fetch a new target page
page:receive
the page has been fetched from the server, but not yet parsed
page:change
the page has been parsed and changed to the new version and on DOMContentLoaded
page:update
is triggered whenever page:change is PLUS on jQuery's ajaxSucess, if jQuery is available (otherwise you can manually trigger it when calling XMLHttpRequest in your own code)
page:load
is fired at the end of the loading process.
Or you can use jquery.turbolinks gem which will trigger jQuery.ready() when Turbolinks triggers the the page:load event.
Update
this is my js code in my rails app
I encapsulate the js code that must be run on every page within a single function
var ready = function(){
$('somEl').on('change', function(){
....
});
$('otherEl').on('click', function(){
....
});
});
and then execute on three event listener
$(document).ready(ready) // jquery event
$(document).on("page:fetch", function(){ // turbolinks event
ready();
// you can add more code here, which will be fired when turbolinks starting fecth new page
}
$(document).on("page:change", function(){ // turbolinks event
ready();
// you can add more code here too
}