【ror学习笔记4】Session

在控制器中,Rails利用cookie维护了一个特殊的,类似于hash的集合,名为session。如果将一个名/值对保存在这个hash中,那么在处理同一个浏览器发出的后续请求时都可以获取到该名/值对。

 

一。配置session

   1。rake db:sessions:create       定义session数据表

  .2。rake db:migrate                  在数据库创建数据表

  .3。告诉rails把session保存在数据库中:

          修改environment.rb以下第三行前#号去掉:

               # Use the database for sessions instead of the file system
               # (create the session table with 'rake db:sessions:create')

               
# config.action_controller.session_store = :active_record_store

 .4。重启服务器

 

二。创建购物车模型和购物车货品模型

1.创建app/models/cart.rb

 
  
class Cart
attr_reader
: items

def initialize
@items
= []
end

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

def total_price
@items
. sum{ | item | item . price}
end


end

2.创建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

 

三。在控制器添加相关方法

 
  
class StoreController < ApplicationController
def index
@products
= Product . find_products_for_sale
end

def add_to_cart
begin
@product
= Product . find(params[ : id])
rescue ActiveRecord
:: RecordNotFound
logger
. error( " Attempt to access invalid product #{params[:id]} " )
redirect_to_index(
" Invalid product " )
else
@cart
= find_cart
@cart
. add_product(@product)
end
end

def empty_cart
session[
: cart] = nil
redirect_to_index(
" Your cart is currently empty " )
end

def redirect_to_index(msg)
flash[
: notice] = msg
redirect_to
: action => : index
end

private
def find_cart
session[
: cart] ||= Cart . new
end
end

 

四。修改布局,修改页面,添加页面

1.app/views/store/index.rhtm添加<%= button_to "Add to Cart",:action=>:add_to_cart,:id=>product%>一句

 
  
< h1 > Your Pragmatic Catalog </ h1 >
<% for product in @products - %>
< div class ="entry" >
< img src ="<%=product.image_url %>" />
< h3 > <% = h(product.title) %> </ h3 >
<% = product.description %>
< div class ="price" > <% = number_to_currency(product.price) %> </ div >
<% = button_to " Add to Cart " ,:action => :add_to_cart,:id => product %>
</ div >
<% end %>

 

 

2.app/views/layouts/store.rhtml修改如下

代码
 
   
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

< html xmlns ="http://www.w3.org/1999/xhtml" xml:lang ="en" lang ="en" >
< head >
< title > Pragprog Books Online Store </ title >
<% = stylesheet_link_tag ' depot' ,:media=>"all"%>
</ head >
< body id = " store " >
< div id = " banner " >
< ! --< img src = " /images/logo.png " />-->
< % = @page_title || " Pragmatic Bookshelf " %>
</ div >
< div id ="columns" >
< div id ="side" >
< a href ="http://www...." > Home </ a >< br />
< a href ="http://www..../faq" > Question </ a >< br />
< a href ="http://www..../news" > News </ a >< br />
< a href ="http://www..../contact" > Contact </ a >< br />
</ div >
< div id ="main" >
<% if flash[:notice] - %>
< div id ="notice" > <% = flash[:notice] %> </ div >
<% end - %>
<% = yield:layout %>
</ div >
</ div >
</ body >
</ html >

 

 3。app/views/store/add_to_cart.rhtml,一按add_to_cart按钮rails就会自动转到这个页面

 
  
< div class ="cart-title" > Your Cart </ div >
< table >
<% for cart_item in @cart.items %>
< tr >
< td > <% = cart_item.quantity %> &times; </ td >
< td > <% = h(cart_item.title) %> </ td >
< td class ="item-price" > <% = number_to_currency(cart_item.price) %> </ td >
</ tr >
<% end %>



< tr class ="total-line" >
< td colspan = "2" > Total </ td >
< td class ="total-cell" > <% = number_to_currency(@cart.total_price) %> </ td >
</ tr >


</ table >


<% = button_to " Empty cart " , :action => :empty_cart %>

 

 

 

转载于:https://www.cnblogs.com/sherlcok/archive/2010/11/19/1882111.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值