Javascript stops working after link_to in rails 4

I have a java script function that allows me to drag and drop a div in my application.js application

  $(function() {
    $( ".draggable" ).draggable();
  });

in my index file I have a bunch of divs with a draggable class,

    <head>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    </head>

<div class="draggable">    
  <p><strong>Title:</strong>
  <%= task.title %></p>
  <p> <%= link_to 'Show', task, :class => 'text' %>
</div>

In addition, each div has a show option, which allows me to go to another page, and on other pages there is a "Back" button that allows me to return to my index page.

<%= link_to 'Back', tasks_url, :class => 'text' %>

but when I return to this page, I can no longer drag the div. If I refresh the page, I can drag the div again.

Is this a conveyor problem that I don't know about?

+4
source share
3 answers

, Turbolinks, , Rails 4. , Turbolinks !

+4

:

$(function() {
  $( ".draggable" ).draggable();
}); 

:

var ready = function() {             
    $( ".draggable" ).draggable();           
};                                   
$(document).ready(ready);            
$(document).on('page:load', ready);

: RailsCasts Turbolinks

+2

It seems to be application.jscached and therefore not executed again after returning. Try putting js to initialize the draggables in the page itself, which needs to be executed yet.

0
source

All Articles