简体   繁体   English

Rails JQuery Ajax:刷新表行引发404错误

[英]Rails JQuery Ajax: refreshing table row throwing 404 error

I have a table of order_items where each order_item is represented on a table row with a button to "set_aside". 我有一个order_items表,其中每个order_item都用“ set_aside”按钮在表行上表示。 The goal is to execute the set_aside controller action for an individual order item and refresh only that table row with an updated table row that contains the new status of the order_item. 目标是对单个订单商品执行set_aside控制器操作,并仅使用包含order_item新状态的更新后的表格行刷新该表格行。

The controller action: 控制器动作:

order_items_controller.rb order_items_controller.rb

def set_aside
  @order_item = OrderItem.find(params[:id])
  respond_to do |format|
    format.js
  end
end

My set_aside.js.erb is: 我的set_aside.js.erb是:

$('#<%= @order_item.id %>').load("<%= j render 'admin/shipments/packing_item_row.haml', locals: {i: @order_item, index: 1} %>");
$('#<%= @order_item.id %>').show();

The view is admin/shipments/packing.haml and contains a table where each table row of the instance variable @order_items is rendered as the partial "packing_item_row" and all table rows have id set. 该视图为admin/shipments/packing.haml ,其中包含一个表,其中实例变量@order_items每个表行都呈现为部分“ packing_item_row”,并且所有表行均设置了ID。

Here's where I am stuck: 这是我被困的地方:

Using the .load() function keeps on throwing an error that I can see in Web Inspector/XHR. 使用.load()函数会不断抛出错误,我可以在Web Inspector / XHR中看到该错误。

Web Inspector shows that the set_aside action correctly includes the order_item.id and the associated HTML in the render call. Web检查器显示set_aside操作正确地在渲染调用中包括order_item.id和关联的HTML。 But then there is a second XHR query that throws a 404 status error by trying to load a admin/shipments/%3Ctr%3E%Ctd resource as well. 但是,还有第二个XHR查询,它通过尝试同时加载admin/shipments/%3Ctr%3E%Ctd资源而引发404状态错误。

Subbing in the .html method seems to load the partial just fine, so it's not a matter of the partial having faulty locals passed in. But the load function simply doesn't seem to work and I am not sure what else to try at this point. 在.html方法中添加字幕似乎可以很好地加载部分函数,​​因此这与传递错误的局部变量的部分函数无关。但是,加载函数似乎根本不起作用,我不确定对此还有什么尝试点。

Can someone point me in the right direction? 有人可以指出我正确的方向吗?

Why this doesn't work 为什么这不起作用

It looks from the jQuery docs that load() accepts a URL, whereas you're giving it some HTML. jQuery文档看来, load()接受一个URL,而您却为其提供了一些HTML。

So that means it's trying to access a URL that's the return value of the HTML. 因此,这意味着它正在尝试访问作为HTML返回值的URL。

That, I suspect, is where the admin/shipments/%3Ctr%3E%Ctd URL is coming from. 我怀疑那是admin/shipments/%3Ctr%3E%Ctd URL的来源。

The %3Ctr%3E%Ctd gobbledegook is Rails returning the HTML to jQuery. %3Ctr%3E%Ctd gobbledegook是Rails将HTML返回到jQuery的结果。

jQuery in turn is trying to make sense of it as a URL. jQuery反过来试图将其理解为URL。 < encoded is %3C - it's taking the start of the first table in your view. <编码为%3C它以视图中第一个表的开头。

Why can't you use html() ? 为什么不能使用html()

It's the easiest way and tried and tested in this scenario. 在这种情况下,这是最简单的方法,并且已经过尝试和测试。 If there's a good reason you don't want to use html() ... 如果有充分的理由,您不想使用html() ...

How to use load() 如何使用load()

If you really want to use load you'll need to create a route for this partial rendering. 如果您确实想使用负载,则需要为此部分渲染创建一条路线。

If the route is called admin_shipments_packing_item_path you'd do something like: 如果该路由称为admin_shipments_packing_item_path您将执行以下操作:

$('#<%= @order_item.id %>').load("<%= j admin_shipments_packing_item_path(@order_item) %>");

Any more questions, just ask! 还有其他问题,问一下即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM