The Depot Application

The Depot Application

8.2 Iteration C1: Creating a Cart 7.4 Iteration B4: Linking to the Cart

8.1 Sessions

rake db:sessions:create
(in /home/rubys/git/awdwr/work/depot)
      invoke  active_record
      create    db/migrate/20100906011143_add_sessions_table.rb
rake db:migrate
mv 20100906011143_add_sessions_table.rb 20100301000004_add_sessions_table.rb
(in /home/rubys/git/awdwr/work/depot)
==  AddSessionsTable: migrating ===============================================
-- create_table(:sessions)
   -> 0.0017s
-- add_index(:sessions, :session_id)
   -> 0.0008s
-- add_index(:sessions, :updated_at)
   -> 0.0009s
==  AddSessionsTable: migrated (0.0036s) ======================================
 
sqlite3 db/development.sqlite3 .schema
CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "description" text, "image_url" varchar(255), "created_at" datetime, "updated_at" datetime, "price" decimal(8,2) DEFAULT 0);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE TABLE "sessions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "session_id" varchar(255) NOT NULL, "data" text, "created_at" datetime, "updated_at" datetime);
CREATE INDEX "index_sessions_on_session_id" ON "sessions" ("session_id");
CREATE INDEX "index_sessions_on_updated_at" ON "sessions" ("updated_at");
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
edit config/initializers/session_store.rb
# Be sure to restart your server when you modify this file.
 
Depot::Application.config.session_store :cookie_store, :key => '_depot_session'
 
# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
Depot::Application.config.session_store :active_record_store

Restart the server.

edit app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery
end
edit app/controllers/store_controller.rb
private
 
  def find_cart
    session[:cart] ||= Cart.new
  end

8.2 Iteration C1: Creating a Cart 7.4 Iteration B4: Linking to the Cart