我正在使用Rails(4.2.6)应用程序,并且遇到了我的某个页面出现的问题。ajax部分渲染后Rails 4窗体恢复为Post(而不是补丁)
有问题的页面包含多种形式,用于帮助生成产品报价的各种模型属性。例如,它允许您设置订单特定的详细信息,如销售数量,保证金百分比等。每个字段都是一个单独的表单,通过ajax提交,并在页面上重新渲染多个部分。 (_costings.html.erb
)
它还允许您从嵌套表格(每个表格在一个表格行中,每个表格都带有一个复选框)中选择要包含在报价中的产品报价数量(以及因此单位的成本) 。在检查/取消选中任何一个框时,它通过jQuery(remote:false)提交相关表单。 (_factory_quotes.html.erb
)
初始加载页面时,产品表单提交完美地工作。我可以根据需要选中/取消选中任意多个框,并且数据将正确提交,并刷新页面。
当我更新任何通过ajax提交的订单值并且部分被重新呈现(但页面未完全刷新)时,问题就出现了。之后,工厂报价单将以邮递方式提交,而不是补丁,导致出现错误,例如[No route matches [POST] "/quotation_quantities/83"
]。
我的假设是,这与某些未被刷新/绑定的东西有关,在ajax调用之后,但我找不到解决方案。
的产品报价数量表主体(_factory_quotes.html.erb):
<tbody>
<% @related_quotes.each do |e| %>
<tr id="<%= e.id %>" class="<%= if e.quantity == @order_costing.order_quantity then
'success'
elsif e.quantity > @order_costing.order_quantity then
'warning '
end %>">
<%= form_for e, method: :put, html: {:id=> "form-#{e.id}"}, remote: false do |quote| %>
<td>
<%= quote.object.quantity %>
</td>
<td>
<%= quote.object.production_lead_time %>
</td>
<td>
<%= Money.new(quote.object.unit_price*100, quote.object.ordercurrency.name).format unless quote.object.unit_price.nil? %>
</td>
<td>
<%= quote.check_box :use_in_costing %>
</td>
<% end %>
</tr>
<script>
$(document).ready(function() {
$('#<%= e.id %>').on('change', function() {
$("#form-<%= e.id %>").submit();
});
});
</script>
<% end %>
</tbody>
例订购特定细节形成(_costings.html.erb):
<div class="row" id="text3">
<div id="text">
<%= form_for @order_costing, remote: true do |f| %>
<div class="col-xs-8">
<%= f.label :gross_margin, 'Margin(%)' %>
</div>
<div class="col-xs-8" %>
<div class=<%= @order_costing.gross_margin <= 20 ? "has-error" : "" %>>
<%= f.number_field :gross_margin, class: 'form-control', :step => 'any' %>
</div>
</div>
<div class="col-xs-4">
<%= f.submit 'Apply', id: "box3", class: 'btn btn-sm btn-default', style: 'float:right' %>
</div>
<% end %>
</div>
</div>
Update.js.erb:
$("#order_costings").html("<%= j render(partial: 'costings') %>");
$("#order_extras").html("<%= j render(partial: 'extras') %>");
$("#order_factory_quotes").html("<%= j render(partial: 'factory_quotes') %>");
$("#order_summary").html("<%= j render(partial: 'summary') %>");
$("#order_quote_quantities").html("<%= j render(partial: 'quote_quantities') %>");
order_costings_controller.html.erb
def update
respond_to do |format|
if @order_costing.update(order_costing_params)
#call a couple of helpers
order_costing_calculations
gather_selected_quote_data
format.html
format.js { render layout: false }
end
end
end
Rails不实际使用HTTP PATCH请求,但一个'_method'-PARAM形式,用POST请求。 –
好的,但它最初记录为一个补丁(在ajax部分渲染之前): '已启动PATCH“/ quotation_quantities/82”for 127.0.0.1 at 2016-09-23 11:03:08 + 0100' 但ajax部分渲染后的帖子: '在2016-09-23 11:04:10 + 0100'处开始POST“/ quotation_quantities/83” 尽管隐藏了'但在两种情况下都存在。 – AlainR