2013-05-08 18 views
0

在我當前的購物車設置中,我允許用戶在他的會話中將項目添加到他的購物車中。購物車獲取會話ID和產品信息並創建一個新的記錄@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 

控制器

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 

回答

1

我可能不正確,但你說過你可能希望有這樣的事情在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 

或者可以嘗試一起工作會以下爲application_controller

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 
+0

我會試試,謝謝 – 2013-05-08 23:23:26

+0

唯一的問題是,如果用戶永遠不會刪除它,我該如何將購物車設置爲「過期」? – 2013-05-08 23:24:10

+0

@derek_duncan查看我的替代方法,可能適合你 – David 2013-05-08 23:56:53