Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Jquery doesn't work on other pages

I believe jQuery doesn't work on other pages than the one you just installed. For example, when I type localhost:3000/, in the '/' directory all jQuery works. But when I click to a link created by Rails as

<%= link_to "Example Link", news_path %>

The page correctly loads, but the jQuery doesn't work. My jQuery codes are as stated:

$(function() {
  console.log( "pageloaded" );
  ....
  $('.up').click(function(){
    .....
  });
});
like image 356
Yagiz Avatar asked Jun 29 '13 14:06

Yagiz


2 Answers

In Rails 4 turbolinks is active by default.
That means, that $(document).ready() is not executed, when you load a new page.

turbolink fires a new event page:load. You can use this to run your javascript code:

$(document).on('page:load', your_start_function);

There is an excelent rails cast on turbolinks

like image 164
Martin M Avatar answered Nov 07 '22 14:11

Martin M


https://github.com/kossnocorp/jquery.turbolinks

This gem binds jQuery.ready function with Turbolinks listen event page:load, therefore the solution is supposed to be solved if you are using Turbolinks with Rails 4

like image 2
Yagiz Avatar answered Nov 07 '22 14:11

Yagiz