关于Session的详细介绍,请仔细参考AWDrR 8.1节的内容。我们暂且认为session是类似于cookie的东西,用来保存用户的访问记录。用户添加到购物车里面的东西,都会放在这个数据结构中。
现在,我们先把session数据保存在数据库中。
>rake db:sessions:create
实施迁移任务。
>rake db:migrate
(in D:/rails/depot)
exists db/migrate
create db/migrate/20081030081220_create_sessions.rb
D:\rails\depot>rake db:migrate
(in D:/rails/depot)
== 20081030081220 CreateSessions: migrating ===================================
-- create_table(:sessions)
-> 0.1090s
-- add_index(:sessions, :session_id)
-> 0.1250s
-- add_index(:sessions, :updated_at)
-> 0.0470s
== 20081030081220 CreateSessions: migrated (0.2810s) ==========================
D:\rails\depot>
默认的session存储机制是基于文件系统的,所以我们还需要更改一些配置文件。打开environment.rb,找到下面几行
# which shouldn't be used to store highly confidential information
# (create the session table with "rake db:sessions:create")
# config.action_controller.session_store = :active_record_store
将最后一行前面的“#”去掉,就会激活数据库的session存储机制。
# which shouldn't be used to store highly confidential information
# (create the session table with "rake db:sessions:create")
config.action_controller.session_store = :active_record_store
还有application.rb文件中的
helper :all # include all helpers, all the time
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => 'f92decc3388dbb79fd1272de8be71b92'
# See ActionController::Base for details
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password
end
把下面的第二行添加上,并去掉:secret前面的“#”。
session :session_key => '_depot_session_id'
helper :all # include all helpers, all the time
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery :secret => 'f92decc3388dbb79fd1272de8be71b92'
# See ActionController::Base for details
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password
end
我们还需要做一些事情:新建一个购物车对象并存放入session中,要保存顾客选好的商品,还要能从session中提取出购物车这个对象。
先在store_controller.rb中创建一个find_cart()方法,简单的实现方式为:
unless session[:cart]
session[:cart] = Cart.new
end
session[:cart]
end
在ruby中,还有一个更为简单的代码,我们抹去刚刚的简单实现方法,重新编写更为简洁的find_cart方法。
app/controllers/store_controller.rb
def find_cart
session[:cart] ||= Cart.new
end
下面我们开始创建购物车。我们创建一个cart.rb文件,放在app\models目录下面。
app/models/cart.rb
attr_reader :items
def initialize
@items = []
end
def add_product(product)
@items << product
end
end
在前面,我们在“Add to Cart”链接中提到了add_to_cart方法,现在我们去实现它。
在app/controllers/store_controller.rb中我们添加add_to_cart方法。
@cart = find_cart
product = Product.find(params[:id])
@cart.add_product(product)
end
现在我们回头点击一下前面的“Add to Cart”按钮。
我们得到了一个“Template is missing”的反馈。在执行add_to_cart方法后,rails回到app\views\stores目录下面去寻找叫add_to_cart的模板。我们先去写一个比较简单的。
app/views/store/add_to_cart.html.erb
<ul>
<% for item in @cart.items %>
<li><%=h item.title %></li>
<% end %>
</ul>
刷新一下。

我们在购物车中间找到了购买信息,似乎离成功又近了一小步。
浙公网安备 33010602011771号