Ruby on Rails-如何删除购物车中的单个ProductItem而又不删除整个购物车中的项目

时间:2019-07-19 20:48:27

标签: ruby-on-rails e-commerce

过程摘要

我正在尝试开发一个简单的电子商务应用程序。我创建了产品,将其转换为产品,然后将这些产品添加到购物车,然后添加到订单

问题

一切正常,如果我想使用以下代码删除购物车中的所有ProductItems:

      <%= link_to 'Empty Your Cart', @cart, method: :delete, data: {confirm: 'You sure?'} %>

但是我似乎无法弄清楚如何删除一个项目而不删除所有项目。

我尝试使用此功能

      <%= link_to 'Delete', @product_item, method: :delete, data: {confirm: 'You sure?'} %>

在我的购物车表格上

<% @cart.product_items.each do|item| %>
       <ul>
        <li>
         <%= item.quantity %>&times;<%= item.product.title %> =
         <%= number_to_currency item.total_price %> - <%=item.product.size %>.

         <%= link_to 'Delete', @product_items, method: :delete, data: {confirm: 'You sure?'} %>

        </li>
       </ul>

  <% end %>

并多次更改控制器,但似乎无济于事。我相信它可能真的很简单,但是我是Rails的新手,似乎无法弄清楚。

这是我的product_items控制器

class ProductItemsController < ApplicationController

  include CurrentCart
  before_action :set_cart, only: [:create, :edit]
  before_action :set_product_item, only: [:show, :index, :new, :destroy, :edit]

  def create
    product = Product.find(params[:product_id])
    @product_item = @cart.add_product(product.id)
    if @product_item.save
      redirect_to products_url, notice: 'Product added to Cart'
    else
    render :new
    end
  end


  def destroy
    @product_item = ProductItem.find(params[:id])
    @product_item.destroy
    redirect_to root_url, notice: 'Order deleted'
  end

  private

  def set_product_item
    @product_item = ProductItem.find(params[:id])
  end

  def product_item_params
    params.require(:product_item).permit(:product_id, :quantity)
  end

end

我的购物车控制器

class CartsController < ApplicationController
before_action :set_cart, only: [:show, :create, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart

def new
 @cart = Cart.new
end

def show
end


def destroy
 @cart.destroy if @cart.id == session[:cart_id]
 session[:cart_id] = nil
 redirect_to root_url, notice: 'Your cart is empty'
end

private

def set_cart
 @cart = Cart.find(params[:id])
end

def cart_params
 params[:cart]
end

def invalid_cart
 logger_error = 'You are trying to access an invalid cart'
 redirect_to product_url,  notice:'Invalid Cart'
end

end

我的产品负责人:

class ProductsController < ApplicationController
  include CurrentCart
  before_action :set_cart, only: [:index, :shop, :show, :about, :women, :kids, :men]
  before_action :set_product, only: [:show, :edit, :update, :destroy]


  def index
    @product = Product.all
  end

  def show
    @product = Product.find(params[:id])
    @photos = Photo.all
  end


  def women
    @women_product_items = Product.women
  end

  def men
    @men_product_items = Product.men
  end

  def kids
    @kids_product_items = Product.kids
  end


  def new
    @product = Product.new
  end

  def edit
  end


  private

    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:title, :photo, :description, :price, :size, :size2, :category)
    end
end

我的cart.rb模型

class Cart < ApplicationRecord

 has_many :product_items, dependent: :destroy

 def add_product(product_id)
  current_item = product_items.find_by(product_id: product_id)

  if current_item
   current_item.quantity += 1

  else
   current_item = product_items.build(product_id: product_id)
  end
   current_item
 end

def total_price
 product_items.to_a.sum{|item| item.total_price}
end

end

1 个答案:

答案 0 :(得分:0)

我认为您已经接近,但您应该将销毁路线更改为特定物品。因此,将@product_items更改为特定的项目item,它是循环内的对象。

<% @cart.product_items.each do|item| %>
   <ul>
    <li>
     <%= item.quantity %>&times;<%= item.product.title %> =
     <%= number_to_currency item.total_price %> - <%=item.product.size %>.

     <%= link_to 'Delete', item, method: :delete, data: {confirm: 'You sure?'} %>

    </li>
   </ul>

<% end %>