【问题标题】:"Delete product from cart" button doesn't work“从购物车中删除产品”按钮不起作用
【发布时间】:2019-02-28 13:04:52
【问题描述】:

我在 Spree 应用中从购物车按钮中删除产品时遇到问题。 我有一个购物车,有部分 _line_item.html.erb:

<%= order_form.fields_for :line_items, line_item do |item_form| %>
  <tr class="first odd">
    <td class="image"><a class="product-image" title="Sample Product" href="#/women-s-crepe-printed-black/">
      <% if variant.images.length == 0 %>
        <%= link_to small_image(variant.product), variant.product %>
      <% else %>
        <%= link_to image_tag(variant.images.first.attachment.url(:small)), variant.product %>
      <% end %>
    </td>
    <td><h2 class="product-name">
      <%= link_to line_item.name, product_path(variant.product) %>
       </h2>
    </td>
    <td class="a-center"><a title="Edit item parameters" class="edit-bnt" href="#configure/id/15945/"></a></td>
    <td class="a-right"><span class="cart-price"> <span class="price">
      <%= line_item.single_money.to_html %></span> </span>
    </td>
    <td class="a-center movewishlist">
      <%= item_form.number_field :quantity, min: 0, class: "form-control line_item_quantity", size: 5 %>
    </td>
    <td class="a-right movewishlist"><span class="cart-price"> <span class="price">
      <%= line_item.display_amount.to_html unless line_item.quantity.nil? %></span> </span>
    </td>
    <td class="a-center last cart-item-delete ">
      <%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-minus-sign'), '#', class: 'delete', id: "delete_#{dom_id(line_item)}" %>
    </td>
  </tr>

<% end %>

最后一个表格单元格是指从购物车按钮中删除产品,但是当点击该购物车时没有任何反应

<%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-minus-sign'), '#', class: 'delete', id: "delete_#{dom_id(line_item)}" %>

【问题讨论】:

  • 它无处链接(因为'#'),也许它应该与js一起使用?我对狂欢一无所知,但这样的链接不起作用
  • 官方文档link_to 表明您应该在id 旁边指定actioncontroller。示例:link_to "Profile", controller: "profiles", action: "show", id: @profile。在您的情况下,&lt;%= link_to content_tag(...), line_item_path(line_item), method: :delete %&gt; 之类的东西也应该可以工作。检查这个很棒的article

标签: ruby-on-rails ruby spree


【解决方案1】:

它可能会起作用:

<%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-minus-sign'), line_item_path(line_item), method: :delete, class: 'delete', id: "delete_#{dom_id(line_item)}" %>

【讨论】:

  • 它不工作。我得到了 NoMethodError: undefined method `line_item_path'
  • 你的config/routes.rb文件怎么样?
  • mount Spree::Core::Engine, :at => '/' 是我的全部
【解决方案2】:

考虑添加

<%= javascript_include_tag "application" %>

到 layouts/spree_application.html.erb

另外,请确保在您的资产管道中添加... assets/javascripts/spree/frontend.js 看起来像这样

    //= require jquery
    //= require jquery_ujs 
    //= require spree
    //= require spree/frontend/checkout
    //= require spree/frontend/product
    //= require spree/frontend/cart
    //= require spree/frontend/locale_selector

这应该可以正常工作:

''

【讨论】:

  • 它仍然无法正常工作。我注意到我将所有 js 文件放在 assets/javascripts/spree/frontend 文件夹中,并且我使用的是 Spree 3.0
猜你喜欢
  • 1970-01-01
  • 2022-06-17
  • 2022-06-16
  • 2020-02-17
  • 2022-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多