agile rails depot 3

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

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 %>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值