Skip to content

Instantly share code, notes, and snippets.

@darthhomme
Created June 7, 2016 19:03
Show Gist options
  • Save darthhomme/3631aa1a5361848043d05695f4c296f9 to your computer and use it in GitHub Desktop.
Save darthhomme/3631aa1a5361848043d05695f4c296f9 to your computer and use it in GitHub Desktop.
class CartController < ApplicationController
def add
id = params[:id]
if session[:cart] then # if a cart has already been created, use it, otherwise make a new one
cart = session[:cart]
else
session[:cart] = {}
cart = session[:cart]
end
if cart[id] then # if the product has been added to the cart, the increase by 1.
cart[id] = cart[id] + 1
else
cart[id] = 1
end
redirect_to :action => :index
end
def clearCart
session[:cart] = nil
redirect_to :action => :index
end
def index
@product = Product
if session[:cart] then
@cart = session[:cart]
else
@cart = {}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment