首页 > 解决方案 > Ajax 发布动态内容导轨 5.0.1

问题描述

我的 Rails 应用程序有问题(5.0.1)

我在quote_edit中有3个模型,quote,quote_materiel和materiel,我对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%> €&lt;/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')) %>");

不是很明白,谢谢你的帮助。

标签: ruby-on-railsajaxrender

解决方案


我解决了这个问题,它是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   

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


推荐阅读