Skip to content

Instantly share code, notes, and snippets.

@andyh
Last active December 15, 2015 13:29
Show Gist options
  • Save andyh/5267931 to your computer and use it in GitHub Desktop.
Save andyh/5267931 to your computer and use it in GitHub Desktop.
class ApplicationController...
def clear_cart
@cart.destroy
session[:cart_id] = nil
end
end
class CartController < ApplicationController
def destroy
@cart = current_cart
clear_cart
respond_to do |format|
format.html { redirect_to root_url, notice: 'Your cart is currently empry' }
format.json { head :no_content }
end
end
end
class LineItemsController < ApplicationController
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
@cart = current_cart
@line_item = @cart.remove_product(params[:id])
respond_to do |format|
if @line_item.save
@line_item.destroy if @line_item.quantity == 0
if @cart.line_items.count.zero?
clear_cart
redirect_to root_url and return
end
format.html { redirect_to @cart, notice: 'Line item was successfully removed' }
format.json { head :no_content }
else
format.html { render action: "new" }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment