small medium large xlarge

Generic-user-small
16 Nov 2008, 06:22
andrew (1 post)

I saw a blog to say to do it this way:

def remove_product(pricing) @items.delete_if { |item| item.pricing == pricing } end

But the problem with that is if you have multiple cart items with the same price, can’t you remove the wrong one?

Anyone have a solid way to do remove individual cart items?

Generic-user-small
16 Nov 2008, 21:50
Tom Monaghan (1 post)

Andrew,

I can’t imagine why you’d do this based on price. Here’s one way that should work.

In your cart model:

  def remove_item(product)
    @items.delete_if { |item| item.product == product }
  end

In your cart controller:

  def remove_item
    @cart = find_cart
    if param_posted?(:id)
      product = Product.find(params[:id])
      @cart.remove_item(product)
      if @cart.items.size > 0
        index
      else
        empty_cart
      end
    end
  end

Best, Tom

You must be logged in to comment