Ruby on rails 3 会话结束时清除表
在我当前的购物车设置中,我允许用户在会话中向购物车添加项目。购物车获取会话ID和产品信息,并创建一个新的Ruby on rails 3 会话结束时清除表,ruby-on-rails-3,session,Ruby On Rails 3,Session,在我当前的购物车设置中,我允许用户在会话中向购物车添加项目。购物车获取会话ID和产品信息,并创建一个新的@cart.lineitems记录。但是,当用户的会话结束时,他们的购物车不会从数据库中删除。这在我的代码中产生了问题,留下了一片混乱。我的问题是,当用户会话结束时,如何删除当前购物车行 代码: 型号 class Views::CartsController < ApplicationController layout 'views' def show @cart
@cart.lineitems
记录。但是,当用户的会话结束时,他们的购物车不会从数据库中删除。这在我的代码中产生了问题,留下了一片混乱。我的问题是,当用户会话结束时,如何删除当前购物车行
代码:
型号
class Views::CartsController < ApplicationController
layout 'views'
def show
@cart = current_cart
end
end
class Views::Cart < ActiveRecord::Base
# attr_accessible :title, :body
has_many :line_items, :dependent => :destroy
has_one :order, :dependent => :destroy
def total_price
# convert to array so it doesn't try to do sum on database directly
line_items.to_a.sum(&:full_price)
end
end
class Views::LineItem < ActiveRecord::Base
attr_accessible :cart, :quantity, :image, :unit_price, :product_id, :album_id
belongs_to :cart
belongs_to :image
accepts_nested_attributes_for :image
def full_price
unit_price * quantity
end
end
类视图::CartsController:销毁
has_one:order,:dependent=>:destroy
def总价
#转换为数组,这样它就不会直接在数据库上求和
行项目至总额(&:全额)
结束
结束
类视图::LineItem
控制器
class Views::LineItemsController < ApplicationController
layout 'views'
def create
@product = Image.find(params[:product_id])
@album = @product.album
attr = {:cart => current_cart, :album_id => @album.id, :product_id => @product.id, :quantity => 1, :unit_price => 10}
@current = Views::LineItem.find_by_product_id(@product.id)
if @current.nil?
Views::LineItem.create(attr)
else
@current.update_attributes(:quantity => @current.quantity + 1, :cart => current_cart)
end
# @line_item = @current ? Views::LineItem.create(:cart => current_cart, :album_id => @album.id, :product_id => @product.id, :quantity => 1, :unit_price => 10) : @current.update_attribute(:quantity, @current.quantity + 1)
flash[:notice] = "Added #{@product.title} to cart."
redirect_to current_cart_url
end
def destroy
@line_item = Views::LineItem.find(params[:id])
if @line_item.quantity > 1
@line_item.update_attribute(:quantity, @line_item.quantity - 1)
else
@line_item.destroy
end
redirect_to current_cart_url
end
end
类视图::LineItemsController当前购物车,:album\u id=>@album.id,:product\u id=>@product.id,:数量=>1,:单价=>10}
@当前=视图::行项目。按产品id(@product.id)查找
如果@current.nil?
视图::LineItem.create(属性)
其他的
@current.update_属性(:quantity=>@current.quantity+1,:cart=>current_cart)
结束
#@line_item=@current?视图::LineItem.create(:cart=>current\u cart,:album\u id=>@album.id,:product\u id=>@product.id,:quantity=>1,:unit\u price=>10):@current.update\u属性(:quantity,@current.quantity+1)
flash[:notice]=“将#{@product.title}添加到购物车。”
将\u重定向到当前\u购物车\u url
结束
def销毁
@line_item=视图::LineItem.find(参数[:id])
如果@line_item.quantity>1
@行项目。更新项目属性(:quantity,@line项目。quantity-1)
其他的
@行_item.destroy
结束
将\u重定向到当前\u购物车\u url
结束
结束
您可以将用户id和会话id存储在购物车行中,然后对其进行范围限定,以仅查看与其当前会话相关的项目。如果要定期清理表,请设置cron作业
我可能不正确,但按照您所说的,您可能希望在
CartsController中有类似的功能
class CartsController < ApplicationController
def destroy
@cart = current_cart #Get current cart
@cart.destroy #call destroy method on the current cart
session[:cart_id] = nil #Ensuring that user is deleting their own cart
respond_to do |format|
format.html { redirect_to(root_url,
:flash => {:error => 'Your cart is currently empty'}) }
end
end
end
唯一的问题是,如果用户从未删除购物车,我如何将购物车设置为“过期”?@derek_duncan可以看到我的替代方法,可能对你有用
def current_cart
if user_signed_in?
if session[:cart_id]
@current_cart ||= current_user.carts.find(session[:cart_id])
if @current_cart.purchased_at || @current_cart.expired?
session[:cart_id] = nil
end
end
if session[:cart_id].nil?
@current_cart = current_user.carts.create!
session[:cart_id] ||= @current_cart.id
end
end
@current_cart
end