1
2
3
4
5
6
7
8
9
10
11
12
13
class LineItemsController < ApplicationController  
  def create
    @product = Product.find(params[:product_id])
    if LineItem.exists?(:cart_id => current_cart.id)
      item = LineItem.find(:first, :conditions => [ "cart_id = #{@current_cart.id}"])
      LineItem.update(item.id, :quantity => item.quantity + 1)
    else  
      @line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price)
      flash[:notice] = "Added #{@product.name} to cart."
    end
    redirect_to root_url
  end  
end