【ror学习笔记4】Session
2010-11-19 18:26
239 查看
在控制器中,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
2.创建app/models/cart_item.rb
三。在控制器添加相关方法
四。修改布局,修改页面,添加页面
1.app/views/store/index.rhtm添加<%= button_to "Add to Cart",:action=>:add_to_cart,:id=>product%>一句
2.app/views/layouts/store.rhtml修改如下
代码
3。app/views/store/add_to_cart.rhtml,一按add_to_cart按钮rails就会自动转到这个页面
一。配置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%>×</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%>
相关文章推荐
- PHP学习笔记【27】--服务端存储技术Session
- session学习笔记
- Session和Cookie学习笔记
- struts2学习笔记--获取Session和request方法
- Cookie 与 session 学习笔记
- Struts学习笔记:request,session,application的创建
- ODAC(V9.5.15) 学习笔记(三)TOraSession(4)
- 【ror学习笔记6】AJAX
- Java for Web学习笔记(二十):Session(4)在集群中使用Session
- PHP 学习笔记 Cookie 与 Session 详解
- servlet学习笔记--如何追踪session
- Mybatis学习笔记--SqlSessionFactory、SqlSession等
- Java for Web学习笔记(十七):Session(1)Session的携带
- YII学习笔记-登录后的session的总结
- php学习笔记(二十八)session的高级管理(基于数据库和memcache的)
- php学习笔记(二十六)创建session
- 菜鸟SpringMVC学习笔记2——@SessionAttributes注解与@RequestMapping
- 【JavaEE学习笔记】Hibernate_06_注解开发,getCurrentSession(),映射关系
- Servlet学习笔记(1)--第一个servlet&&三种状态对象(cookie,session,application)&&Servlet的生命周期
- 黑马程序员-- .Net学习日记——ASP.net学习笔记:Application、Session、Server