appointment: this demo ran in ubuntu, and Rails 2.2.2
the demo comes from agile web development with rails 3
script>>some linux script
mysql>some db command
---------------------------------------------------------
Cart Creation --important
---------------------------------------------------------
step 1
putting sessions in the databases
script>>rake db:sessions:create
script>>rake db:migrate
step 2
use database to create session instead of the default
modify config/environment.rb
if not use cookie store , config app/controllers/application.rb
step 3
now start to modify the code
views/store/index.html.erb
app/models/cart.rb
app/controllers/store_controller.rb
app/views/store/add_to_cart.html.erb
[b]step 4 [/b]
smarter cart
app/models/cart.rb
views/store/add_to_cart.html.erb
clear sesison
script>>rake db:sessions:clear
[b]step 5 [/b]
handling Errors
display error message
views/layouts/store.html.erb
[b]step 5 [/b]
Empty cart
views/store/add_to_cart.html.erb
app/controllers/store_controller.rb
point the image to add cart
the demo comes from agile web development with rails 3
script>>some linux script
mysql>some db command
---------------------------------------------------------
Cart Creation --important
---------------------------------------------------------
step 1
putting sessions in the databases
script>>rake db:sessions:create
script>>rake db:migrate
mysql> desc sessions;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| session_id | varchar(255) | NO | MUL | NULL | |
| data | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | MUL | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
step 2
use database to create session instead of the default
modify config/environment.rb
config.action_controller.session_store = :active_record_store
if not use cookie store , config app/controllers/application.rb
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery :secret => 'efaaf2f5cc7eb43de9037be1aa8916fb'
step 3
now start to modify the code
views/store/index.html.erb
<%= button_to "Add to Cart" , :action => 'add_to_cart', :id => product %>
app/models/cart.rb
class Cart
attr_reader :items
def initialize
@items = []
end
def add_product(product)
@items << product
end
end
app/controllers/store_controller.rb
class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale
end
def find_cart
session[:cart] ||= Cart.new
end
def add_to_cart
product = Product.find(params[:id])
logger.error( product);
@cart = find_cart
@cart.add_product(product)
end
private :find_cart
end
app/views/store/add_to_cart.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>
<% for item in @cart.items %>
<li><%=h item.title %></li>
<% end %>
</ul>
[b]step 4 [/b]
smarter cart
app/models/cart_item.rb
class CartItem
attr_reader :product, :quantity
def initialize(product)
@product = product
@quantity = 1
end
def increment_quantity
@quantity += 1
end
def title
@product.title
end
def price
@product.price * @quantity
end
end
app/models/cart.rb
def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end
end
views/store/add_to_cart.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>
<% for item in @cart.items %>
<li><%= item.quantity %> × <%=h item.title %></li>
<% end %>
</ul>
clear sesison
script>>rake db:sessions:clear
[b]step 5 [/b]
handling Errors
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid product #{params[:id]}" )
flash[:notice] = "Invalid product"
redirect_to :action => 'index'
end
display error message
views/layouts/store.html.erb
<div id="main">
<% if flash[:notice] -%>
<div id="notice"><%= flash[:notice] %></div>
<% end -%>
<%= yield :layout %>
</div>
[b]step 5 [/b]
Empty cart
views/store/add_to_cart.html.erb
<%= button_to 'Empty cart', :action => 'empty_cart' %>
app/controllers/store_controller.rb
def empty_cart
session[:cart] = nil
flash[:notice] = "Your cart is currently empty"
redirect_to :action => 'index'
end
point the image to add cart
<% form_remote_tag :url => { :action => 'add_to_cart', :id => product } do %>
<%= image_submit_tag(product.image_url) %>
<% end %>