更新或从购物车中删除项目?

时间:2019-06-19 16:55:34

标签: ruby-on-rails ruby

我管理客户的订单。我的Order表已连接到模型Cart。模型Cart连接到Product模型Many to Many,后者给出了关联表line_items,其中包括id_cartid_product和{{1 }}。但是对于quantity,我想将选项添加到Cart并将update的元素delete中添加。我遇到了一个障碍-更确切地说是在视图Cart中,在该视图的最后,我显示了Order中的元素列表,如下所示:

Cart

我将在该字段中输入默认值,该字段是客户订购的允许修改的默认值。但是问题在于,服装<h3> Order Details </ h3> <table class = "table-bordered table"> <td> Designation </ td> <td> Quantity </ td> <td> Price </ td> </ tr> <% order.cart.line_items.each do | item | %> <tr align = "center"> <td> <% = f.text_field: product_id,: value => item.product.title,: readonly => true, class: 'form-control'%> </ td> <td> <% = f.text_field: qte,: value => item.qte, class: 'form-control'%> </ td> <td> <% = number_to_currency (product.inc.unit, unit: "F", separator: ",", format: "% n% u")%> </ td> </ Tr> <% end%> </ Table> product_id不属于模型qte,而更早属于模型order,因此我无权插入:< / p>

line_items

我什至尝试使用方法unpermitted parameters: :product_id, :qte ,我将获得许可。但是事务不会提交,只能回滚。

感谢您帮助我解决了导致我几天无法前进的错误

提交的参数看起来像这样

accepts_nested_attributes_for

我的控制器订单中有这个

    <%= form_with(model: order,:remote => true)  do |f| %>
<div class="content">
<div class="container-fluid">
  <div class="row">
      <div class="col-md-12">
          <div class="card">
      <div class="header">
          <h4 class="title"> Edite Order</h4>
      </div>

      <div class="content">
          <div class="row">
            <!--  Order Reference-->
              <div class="col-md-5">
                  <div class="form-group">
                      <label><%= f.label :ref,"Reference Order "%></label>
                      <%= f.text_field :ref, :value => "ABC-123-NY#{@order.id}",:readonly => true,class: ' form-control' %>
                  </div>
              </div>
              <!-- Supplier List -->
              <div class="col-md-5">
                  <div class="form-group">
                      <label><%= f.label :supplier_id,"Fournisseur"%></label>
                      <%=f.collection_select :supplier_id,Fournisseur.all,
                    :id,:namefrs,{required: true},id:"listfrs",class: 'form-control'%>
                  </div>
              </div>
              <!-- boutton update -->
              <div class="col-md-2">
                  <div class="form-group">
                       <%= f.submit 'Modifier',:name => "valid",:class => 'btn btn-info btn-fill pull right'%>
                  </div>
              </div>


             </div>
         </div>
       </div>

       <div class="card">
      <div class="content">
          <div class="row">
            <!-- Date Order -->
              <div class="col-md-5">
                  <div class="form-group">
                      <label><%= f.label :date_order,"Date"%></label>
                      <%= f.date_select :date_order, label: false, placeholder: "Sélectionnez la date et l'heure",class: 'form-control'%>
                  </div>
              </div>
             </div>
         </div>
       </div>

      </div>
    </div>
</div>
</div>

      <div class="col" id="cart">
              <h3>Order</h3>
    <table class="table table-bordered">
    <tr align="center" class="h4">
    <td>Designation</td>
    <td>Quantity</td>
    <td>Price</td>
    </tr>

    <% order.cart.line_items.each do |item| %>
    <tr align="center">

        <td><%= f.text_field :product_id,:value => item.product.title,:readonly => true,class: 'form-control'%></td>
        <td><%= f.text_field :qty, :value=>item.qty ,class: 'form-control' %></td>
        <td><%= number_to_currency(item.product.price, unit: "F",separator:",",format:"%n %u")%></td>
    </tr>


<% end %>
   </table>
      </div>

    <% end %>

0 个答案:

没有答案