动态内容栏5.0.1上的Ajax发布

时间:2018-12-17 15:53:05

标签: ruby-on-rails ajax render

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

我有3个模型,quotation,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>

  

这看起来像这样

enter image description here

到目前为止,我可以通过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')) %>");

我不太了解,谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

现在所有的工作,谢谢您的帮助