End chapter 8
[depot.git] / app / controllers / store_controller.rb
1 class StoreController < ApplicationController
2 def index
3 @products = Product.find_products_for_sale
4 end
5
6 def add_to_cart
7 product = Product.find(params[:id])
8 @cart = find_cart
9 @cart.add_product(product)
10 rescue ActiveRecord::RecordNotFound
11 logger.error("Attempt to access invalid product #{params[:id]}" )
12 redirect_to_index('Invalid product')
13 end
14
15 def empty_cart
16 session[:cart] = nil
17 redirect_to_index('Your cart has been emptied')
18 end
19
20 private
21
22 def find_cart
23 session[:cart] ||= Cart.new
24 end
25
26 def redirect_to_index(msg)
27 flash[:notice] = msg
28 redirect_to :action => 'index'
29 end
30
31 end