The Depot Application
8.1 Sessions
7.4 Iteration B4: Linking to the Cart
8.1 Sessions
rake db:sessions:create
(in /home/rubys/git/awdwr/work-191/depot)
invoke active_record
create db/migrate/20100211191208_add_sessions_table.rb
rake db:migrate
mv 20100211191208_add_sessions_table.rb 20100301000004_add_sessions_table.rb
(in /home/rubys/git/awdwr/work-191/depot)
== AddSessionsTable: migrating ===============================================
-- create_table(:sessions)
-> 0.0012s
-- add_index(:sessions, :session_id)
-> 0.0004s
-- add_index(:sessions, :updated_at)
-> 0.0003s
== AddSessionsTable: migrated (0.0021s) ======================================
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.
# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
ActionController::Base.session = {
:key => '_depot_session',
:secret => '8c3ecf1db82a2129015f77a059189e070ca50a2c6360e3f5f53bbb9a93bb9cb2a3b31b354d307d44c7b7eb1c9d5a89eea1a67f70aa846681fdfe4c9edb324628'
}
# 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 "rake db:sessions:create")
ActionController::Base.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