Rails AJAX request is returned 5 times instead of one
I'm trying to do a simple AJAX request using link_to :remote option and display t开发者_高级运维he response dynamically. The problem is that I get 5 responses instead of one. Why this might be happening?
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'm using jQuery. Gemset includes Devise, paperclip and simple_form. I'm having also similar problem when using :confirm with link_to. The thing is that this confirmation dialog is then displayed 5 times regardless of what you press.
There is only one item with 'sortable' id in the generated html:
<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>
You got this problem with different links, right?
Your link handler has been registered multiple times (you can easily check that by using firebug in firefox or the developer tools in chrome, just click the link and look up how many requests are getting sent up, or console.log the handler)
This can happen if you register remote links dynamically (after an asynchronous page fragment load, for instance) and the easiest fix is then to mark your link as registered (for example, by adding a class 'registered') and not register those again, like this
# Link registration
$('a.my_link:not(.registered)').click(function(e) {
#your code here
}).addClass('registered');
If you have not yourself written any click event handlers in jQuery, check for libraries doing that for you. Look for something like:
$(document).ajaxComplete(function() {
$('a[data-remote="true"]').click(function() {
#some code here...
});
});
Thanks! That was pretty much the case. I checked the code and here is the problem causing that behaviour:
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' %>
The thing is that I'm including here 'rails' and 'application' scripts with every other js file. Here is the fix:
<%= 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' %>
Removing those solved multiple AJAX GET requests, as well as :confirm dialogs displaying multiple times.
Also learned how to use Firebug for AJAX debugging :D Thanks!
I had the same problem when using the asset pipeline. assets:precompile
copies the content of all js files into the application.js, when you now open the page in the browser and the application.js
file gets cached, and start again in developent, the browser will load the application.js
and the jquery_ujs.js
file at the same time. So there are 2 remote handlers registered at the same time.
Cleaning the browser cache was the solution for me. (Maby this will help some others running in to similar problems)
精彩评论