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>
step 4
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
step 5
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>
step 5
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 %>
分享到:
相关推荐
《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...
《Agile Web Development with Rails》(敏捷Web开发:Ruby on Rails)这本书,作为Rails开发新手的教材,强调了敏捷开发方法,并以其帮助开发者建立起一个实用的Web应用。从给出的文件信息来看,这本书正在编写过程...
### 敏捷Web开发与Rails 3:关键知识点解析 #### 一、Rails版本与兼容性 本书《敏捷Web开发与Rails》第三版是基于Rails 2编写的。截至本书印刷时,当前可用的Rails Gem版本为2.1。书中所包含的所有代码均已在该...
#### 3. ActiveRecord:Rails的模型支持 ActiveRecord是Rails中一个重要的ORM(对象关系映射)库,它提供了数据库交互的高级抽象,使得开发者能够以面向对象的方式操作数据库记录,而无需编写繁琐的SQL语句。...
总而言之,《敏捷Web开发与Rails》第四版是一本全面覆盖Rails 3的教程,它不仅教授了Rails框架的基础知识,还深入探讨了敏捷开发的最佳实践,对于任何想要掌握或提升Rails技能的人来说,都是一本不可或缺的参考资料...
此外,第二版还深入讨论了Rails 3.x和4.x中的路由系统,以及如何使用Unobtrusive JavaScript(UJS)实现更干净、分离的前端代码。 这两本书都强调了敏捷开发的原则,如迭代开发、持续集成、用户故事和重构。它们...
3. **版本检查方法**: - 要检查当前正在使用的Rails版本,可以通过命令行输入`rails -v`来查看版本号。 4. **作者团队简介**: - 本书由多位知名的Rails开发者共同编写,包括Sam Ruby、Dave Thomas、David ...
3. **Turbolinks**: Rails 4引入了Turbolinks,这是一个用于加速页面加载的技术。它通过只替换页面的部分内容而不是整个页面,显著减少了页面刷新的等待时间,提高了用户体验。 4. **Asset Pipeline**: 资产管道是...
### Agile Web Development with Rails for Rails 3.2 #### 核心知识点概览 - **Rails 3.2概述** - **敏捷开发方法论** - **Model-View-Controller (MVC) 模式** - **Ruby on Rails基础与高级特性** - **面向对象...
3. **Rails 2 新功能介绍**: - 深入探讨 Rails 2 引入的新功能,如 Active Record 的改进、RESTful 设计等。 - 阐释了这些新功能的实际应用案例。 4. **实践指南**: - 提供了基于 Rails 2 构建实际项目的详尽...
agile web development with rails 5(英文电子书).............................................................................................................................................................
Agile Web Development with Rails 1-14节_ppt(老师发的修正版)