【问题标题】:Ajax post on dynamic content rails 5.0.1Ajax 发布动态内容导轨 5.0.1
【发布时间】:2018-12-17 15:53:04
【问题描述】:

我的 Rails 应用 (5.0.1) 有问题

我有 3 个模型,quote,quotation_materiel 和 materiel 在quotation_edit 中,我对quotation_materiel 搜索进行了渲染。 当

引用编辑:

#collapseMaterial.collapse.in
.col-md-8.col-md-offset-2
 %h3 Recherche de matériel
 .card.card-body
  = form_tag edit_client_quotation_path, method:[:get, :post], remote: true, :id => "quotations_search" do
    .input-group
      = text_field_tag :search,
        params[:search],
        autocomplete: :off,
        placeholder: 'Recherche de matériel',
        class: 'form-control search-input'

    #quotation_matos= render 'quotations'

%input#mon_devis_id.hidden{:type => "text", :value => "#{@quotation.id}"}
%input#mon_client_id.hidden{:type => "text", :value => "#{params[:client_id]}"}

我的报价呈现:

<table class="table table-stripped">
<thead>
<tr>
  <th class="text-center">Désignation</th>
  <th class="text-center">Qté</th>
  <th class="text-center">Remise <br>
                          (%)    </th>
  <th class="text-center">PU HT</th>
</tr>

<% for quotation_materiels in @quotation_materiels%>
  <tr>
    <% @remise = ((quotation_materiels.floor_price - quotation_materiels.price) / (- quotation_materiels.price) )*100%>
    <td class="text-center"><b><%= quotation_materiels.ref %></b><br>
                            <%= quotation_materiels.name %></td>
    <td class="text-center"><%= select_tag :quantity, options_for_select(1..12), id: "set_quantity_#{quotation_materiels.id}", class: "form-control" %></td>
    <td class="text-center"><%= select_tag :remise, options_for_select(0..@remise), include_blank: true, id: "set_remise_#{quotation_materiels.id}", class: "form-control" %></td>
    <td class="text-center"><%= quotation_materiels.price%> €</td>
    <td class="text-center"><button class="add_quotation btn btn-success" value="<%= quotation_materiels.id %>" data-remote="true" >Ajouter au devis</button></td>
  </tr>
<% end %>

<table class="table table-stripped">
  <thead>
    <tr>
      <th class="text-center">Désignation</th>
      <th class="text-center">Qté</th>
      <th class="text-center">Remise <br>
                              (%)    </th>
      <th class="text-center">Prix unitaire HT</th>
      <th class="text-center">Prix de vente HT</th>
      <th class="text-center">Supprimer</th>
    </tr>
  </thead>
  <tbody id="matos_lists">

    <% @materiel_added_to_quotations.each do |materiel|%>
      <tr>
        <%= render "materiel_row", materiel:materiel %>
      </tr>
    <% end %>
  </tbody>

在 materiel_row 渲染中:

<% if materiel %>
<tr class="rover" id="<%= "removed_#{materiel.id }"%>">
<td class="text-center"> <b><%= materiel.name %></b><br>
                         <%= materiel.designation_devis %></td>
<td class="text-center"><%= materiel.quantity%></td>
<% if materiel.remise != 0 %>
  <td class="text-center"><%= materiel.remise%> </td>
<% else %>
  <td></td>
<%end%>
<td class="text-center"><%= materiel.prix_unitaire_ht %></td>
<td class="text-center"><%= materiel.prix_vente_ht%></td>
<td class="text-center"><button value="<%= materiel.id%>" onclick="remove_matos(<%= materiel.id%>)" class="delete_materiel btn btn-danger"> Supprimer</button></td>

看起来像这样:

到目前为止一切顺利,我可以通过 ajax 发布,但是当我进行搜索时出现问题,我的 ajax 请求在我的:

<button class="add_quotation btn btn-success" value="<%= quotation_materiels.id %>" data-remote="true" >Ajouter au devis</button></td>

我得到了:

Started POST "/clients/2/quotations/81/edit" for 127.0.0.1 at 2018-12-17 16:39:29 +0100

ActionController::RoutingError (No route matches [POST] "/clients/2/quotations/81/edit"):

如果我不搜索我的请求 ajax 工作:

 INSERT INTO `materiels` (`name`, `quantity`, `created_at`, `updated_at`, `designation`, `prix_unitaire_ht`, `prix_vente_ht`, `fourniture`, `designation_devis`, `prix_sans_remise`, `quotations_id`) VALUES ('00001', 1, '2018-12-17 16:43:48', '2018-12-17 16:43:48', '', 235.0, 235.0, 1, 'Caméra IP', 235.0, 81)

我的 ajax 请求:

$(document).ready(function () {
 $(".add_quotation").click( function (){
  var mon_client_id = $("#mon_client_id").val();
  var materiel_id = $(this).val();
  var quantity = $("#set_quantity_" + materiel_id).val();
  var remise = $("#set_remise_" + materiel_id).val();
  var mon_devis_id = $("#mon_devis_id").val();
 $.ajax({
  url : '/add_to_quotation', // La ressource ciblée
  type : 'POST', // Le type de la requête HTTP.
  data:{
    materiel_id: materiel_id,
    mon_client_id: mon_client_id,
    mon_devis_id: mon_devis_id,
    ma_quantity: quantity,
    ma_remise: remise
  },
  success: (function(data) {
    if (data.materiel){
      $("#matos_lists").append(data.materiel);
      $("#total_price").html(data.total_price + " €");
      $("#total_price_TVA").html(data.total_price_TVA + " €");
      $("#total_price_1").html(data.total_price_1 + " €");
      $("#total_price_TVA_1").html(data.total_price_TVA_1 + " €");
    }
    if (data.type == "error") {
      window.alert(data.message)
    }
    else if(data.type == "info"){
      window.alert(data.message)
    }
})
}).fail(function() {
  alert("An error has occurred trying to process your request. Please try again later ");
 });
});

我的edit.js.erb:

$("#quotation_matos").html("<%= escape_javascript(render('quotations')) %>");

我不太明白,谢谢你的帮助。

【问题讨论】:

  • 我也有同样的问题,你能把你的 routes.rb 贴出来吗?
  • 你的路线在哪里
  • 您的问题中有大量不必要的信息。您应该尝试将其缩减为基本要素。看来您正在尝试解决ActionController::RoutingError。如果是这种情况,有两件事需要立即检查。 (1) 您的请求是否正确形成? (换句话说,对你真的意味着向"/clients/2/quotations/81/edit"发出POST请求?向以edit结尾的路由发出POST请求是非常规的。)并且,(2)您的路线是否正确形成?如前所述,您的 routes.rb 会有所帮助。

标签: ruby-on-rails ajax render


【解决方案1】:

我解决了这个问题, 是haml压痕造成的:

%input#mon_devis_id.hidden{:type => "text", :value => "#{@quotation.id}"}
%input#mon_client_id.hidden{:type => "text", :value => "#{params[:client_id]}"}

在我的 #quotation_matos= 上渲染“引用”

所以当我搜索和添加产品时,我会得到一个带有这个参数的 204 json 响应:

materiel_id 5
ma_quantity 1
ma_remise   

我只是将我的输入从我的渲染中移出,我得到一个 200 json 响应:

materiel_id 5
mon_client_id 2
mon_devis_id 90
ma_quantity 1
ma_remise   

现在一切正常,感谢您的帮助

【讨论】:

    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多