参照敏捷开发 4 中的代码。定义了购物车,我另外加了个devise
来验证用户,可是到我退出再登录的时候,购物车就自动清空了。不能保存上次的添加进购物车的内容。
购物车已经存进数据库了。
现在就不知道怎么购物车跟当前的用户关联起来。
这个是加入的购物车的定义的内容
class CartsController < ApplicationController
before_filter :extract_cart
def create
@product = Product.find(params[:product_id])
@cart.add(@product, @product.title)
redirect_to cart_path
end
private
def extract_cart
cart_id = session[:cart_id]
@cart = session[:cart_id] ? Cart.find(cart_id) : Cart.create
session[:cart_id] = @cart.id
end
这个要怎么让用户加入购物车的时候抓到用户的 ID,让下次登录的时候也能看到购物车的内容
这个是CartItems
表中的字段
CartItem Load (0.4ms) SELECT "cart_items".* FROM "cart_items"
=> [#<CartItem id: 1, owner_id: 7, owner_type: "Cart", quantity: 6, item_id: 1, item_type: "Product", price: 0.0, created_at: "2013-09-03 06:09:06", updated_at: "2013-09-03 06:30:04", user_id: nil>, #<CartItem id: 2, owner_id: 7, owner_type: "Cart", quantity: 1, item_id: 3, item_type: "Product", price: 0.0, created_at: "2013-09-03 06:14:47", updated_at: "2013-09-03 06:14:47", user_id: nil>, #<CartItem id: 3, owner_id: 7, owner_type: "Cart", quantity: 3, item_id: 4, item_type: "Product", price: 123.0, created_at: "2013-09-03 06:15:25", updated_at: "2013-09-03 06:32:13", user_id: nil>, #<CartItem id: 4, owner_id: 7, owner_type: "Cart", quantity: 1, item_id: 6, item_type: "Product", price: 333.0, created_at: "2013-09-03 06:15:41", updated_at: "2013-09-03 06:15:41", user_id: nil>, #<CartItem id: 5, owner_id: 7, owner_type: "Cart", quantity: 3, item_id: 5, item_type: "Product", price: 222.0, created_at: "2013-09-03 06:15:45", updated_at: "2013-09-03 06:18:00", user_id: nil>, #<CartItem id: 6, owner_id: 8, owner_type: "Cart", quantity: 1, item_id: 1, item_type: "Product", price: 0.0, created_at: "2013-09-03 06:36:00", updated_at: "2013-09-03 06:36:00", user_id: nil>, #<CartItem id: 7, owner_id: 8, owner_type: "Cart", quantity: 1, item_id: 2, item_type: "Product", price: nil, created_at: "2013-09-03 06:36:05", updated_at: "2013-09-03 06:36:05", user_id: nil>, #<CartItem id: 8, owner_id: 8, owner_type: "Cart", quantity: 2, item_id: 3, item_type: "Product", price: 0.0, created_at: "2013-09-03 06:36:10", updated_at: "2013-09-03 06:36:10", user_id: nil>]
这个是要怎么解决,还希望各位大牛指导