/
carts_controller.rb
90 lines (79 loc) · 2.09 KB
/
carts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class CartsController < ApplicationController
# GET /carts
# GET /carts.json
def index
@carts = Cart.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @carts }
end
end
# GET /carts/1
# GET /carts/1.json
def show
begin
@cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
else
respond_to do |format|
format.html # show.html.erb
format.json { render json: @cart }
end
end
end
# GET /carts/new
# GET /carts/new.json
def new
@cart = Cart.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @cart }
end
end
# GET /carts/1/edit
def edit
@cart = Cart.find(params[:id])
end
# POST /carts
# POST /carts.json
def create
@cart = Cart.new(params[:cart])
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render json: @cart, status: :created, location: @cart }
else
format.html { render action: "new" }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# PUT /carts/1
# PUT /carts/1.json
def update
@cart = Cart.find(params[:id])
respond_to do |format|
if @cart.update_attributes(params[:cart])
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart = current_cart
@cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url,
notice: 'Your cart is currently empty' }
format.json { head :no_content }
end
end
end