Rails的AJAX请求返回的5倍,而不是一而不是、Rails、AJAX

2023-09-10 16:49:50 作者:哈里波特大

我想要做的link_to使用一个简单的AJAX请求:远程选项和动态显示的响应。问题是,我得到5答复,而不是一个。为什么会是这样吗?

page.html.erb:

 <%=的link_to item.title,item_path(项目:格式=>:JS):远程=>真%>
 

show.js.erb:

  $(<%= escape_javascript渲染(:文件=>项目/ show.html.erb)%>中)。insertAfter('#可排序) ;
$('#show_item')了slideDown();
 

items_controller.rb:

 高清节目
        @item = Item.find(PARAMS [:ID])
        respond_to代码做|格式|
            的format.html
            format.js
        结束
    结束
 
Google Maps应用程序Rails和Ajax开发指南

更新:我使用jQuery。宝石包括设计,回形针和simple_form。我也有类似的问题,当使用:确认的link_to。问题是,就显示此确认对话框,无论你preSS的5倍。

,只有一个项目与'可排序'身份证在生成的HTML

 < UL ID =排序>
        <李类=UI状态默认><跨度类=UI图标UI图标-arrowthick-2-NS>< / SPAN>< A HREF =/项目/ 10 .js文件数据的远程=真正的>另一个项目< / A>< /李>
        <李类=UI状态默认><跨度类=UI图标UI图标-arrowthick-2-NS>< / SPAN>< A HREF =/项目/ 9 .js文件数据的远程=真正的>测试< / A>< /李>
     < / UL>
 

解决方案

您有这个问题,不同的环节,对吧?

您联系处理程序已被注册多次(你可以很容易地检查,通过用Firebug在Firefox或Chrome浏览器的开发者工具,只需点击链接,并期待有多少要求越来越送上去,或者CONSOLE.LOG处理程序)

如果你动态注册远程链路(异步页片段加载后,例如)这可能发生,最简单的解决方法是再标记您的注册链接(例如,通过添加类注册),而不是注册这些再次,像这样

 #链接注册
$('a.my_link:没有(.registered))点击(函数(E){。
  #your code在这里
})addClass(注册)。
 

如果您还没有自己写任何单击事件处理程序的jQuery,检查这样做对你的库。寻找类似:

  $(文件).ajaxComplete(函数(){
  $('A [数据远程=真])。点击(函数(){
    #some code在这里...
  });
});
 

I'm trying to do a simple AJAX request using link_to :remote option and display the 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...
  });
});