format.js无法在rails 2中工作

时间:2013-04-05 08:25:22

标签: ruby-on-rails

我有两个名为client/ordersnotary/orders的控制器。在“客户端/订单”中有click_me链接。当我点击链接时,我应该将ajax请求传递给notary/orders/pending_ordersreload the notary/orders/pending_orders页。

这是我在client / orders / new

中的Ajax Call
<script>
    jQuery.noConflict();
jQuery(document).ready(function(){
alert(9);
jQuery(".test_link").live("click", function(){
  alert("am clicked");
      jQuery.ajax({
      url: "/notary/orders/pending_orders",
      dataType: 'script',
      type: "get",
      data: {data:'hi'},
      success:function(){
          alert("success");
      },
      error:function(){
          alert("failure");   
      }
    });
})

});

<%= link_to "click me", "javascript:void(0);" , :class => "test_link" %>

这是我在notary/orders/pending_orders

中的代码
if request.xhr?
   respond_to do |format|
   format.js { render :js=>'location.reload();',
   :location => "notary/orders/pending_orders"}
   end
    end

但它重新加载client/orders/new而非notary/orders/pending_orders。但我想重新加载 公证人/ orders / pending_orders。我该怎么做?

1 个答案:

答案 0 :(得分:1)

我想链接<%= link_to "click me", "javascript:void(0);" , :class => "test_link" %>client/orders/new,您想要从notary/orders/pending_orders更新页面client/orders/new

由于您是从client/orders/new提出请求,因此您的所有回复都只会在client/orders/new中得到更新。

您似乎正在client/orders/new从一个浏览器执行操作,并在另一个浏览器中侦听页面notary/orders/pending_orders,以便在后端发生某些更改时更新通知。

要在后端发生更改时自动更新页面(例如:在notary/orders/pending_orders中显示通知),我要做的是,我会为这样的事情设置一个单独的监听器,< / p>

    <script type="text/javascript">
      jQuery(document).ready(function(){
        notificationListener();
      }); 

      function notificationListener(){
        jQuery.ajax({
           url: "/notary/orders/pending_orders",
           type: "get",
           data: {data:'yourdata'},
           success:function(){
             if(expected result){
                //code to update browser view
             }
             else{
                setTimeout(notificationListener(),10000)
             }
           },
           error:function(){
              //
           }
      });
    }

</script>