Rails AJAX request returns 5 times instead of one

I am trying to execute a simple AJAX request using link_to: remote and display the response dynamically. The problem is that I get 5 responses instead of one. Why can this happen?

page.html.erb:

<%= link_to item.title, item_path(item, :format => :js), :remote => true %>

show.js.erb:

$("<%= escape_javascript render(:file => 'items/show.html.erb') %>").insertAfter('#sortable');
$('#show_item').slideDown();

items_controller.rb:

def show
        @item = Item.find(params[:id])
        respond_to do |format|
            format.html
            format.js
        end
    end

Update: I am using jQuery. Gemset includes Devise, paperclip and simple_form. I also have a similar problem when using: confirm with link_to. The fact is that this confirmation dialog is displayed 5 times no matter what you click.

There is only one element in the generated html with the 'sortable' id:

<ul id="sortable"> 
        <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><a href="/items/10.js" data-remote="true">Another item</a></li> 
        <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><a href="/items/9.js" data-remote="true">test</a></li> 
     </ul> 
+5
source share
3 answers

, ?

( , firebug firefox chrome , , console.log )

, (, ), , (, "" ) ,

# Link registration
$('a.my_link:not(.registered)').click(function(e) {
  #your code here
}).addClass('registered');

- jQuery, , . - :

$(document).ajaxComplete(function() {
  $('a[data-remote="true"]').click(function() {
    #some code here...
  });
});
+3

! . , :

application.html.erb

 <%= javascript_include_tag 'jquery-1.4.4.min', 'rails', 'application' %>
  <%= javascript_include_tag 'jquery-ui-1.8.9.custom.min', 'rails', 'application' %>
  <%= javascript_include_tag 'jquery.prettyPhoto', 'rails', 'application' %>
  <%= javascript_include_tag 'jquery.fancybox-1.3.4.pack', 'rails', 'application' %>
  <%= javascript_include_tag 'placeholder', 'rails', 'application' %>

, "rails" "application" js . :

  <%= javascript_include_tag 'jquery-1.4.4.min', 'rails', 'application' %>
  <%= javascript_include_tag 'jquery-ui-1.8.9.custom.min' %>
  <%= javascript_include_tag 'jquery.prettyPhoto' %>
  <%= javascript_include_tag 'jquery.fancybox-1.3.4.pack' %>
  <%= javascript_include_tag 'placeholder' %>

AJAX GET, : .

, Firebug AJAX: D !

0

. assets:precompile js application.js, , application.js , application.js jquery_ujs.js . , 2 .

. ( , )

0
source

All Articles