【问题标题】:Clear Table when Session Ends Rails会话结束时清除表格 Rails
【发布时间】:2025-12-18 19:25:02
【问题描述】:

在我当前的购物车设置中,我让用户能够在他的会话中将商品添加到他的购物车中。购物车获取会话 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

【问题讨论】:

    标签: ruby-on-rails-3 session


    【解决方案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
    

    【讨论】:

    • 唯一的问题是,如果用户从不删除它,我如何将购物车设置为“过期”?
    • @derek_duncan 看看我的替代方法可能对你有用
    【解决方案2】:

    您可以将 user_id 和 session_id 存储在您的购物车行中,然后将其范围限定为仅查看与其当前会话相关的项目。如果您想定期清理表,请设置一个 cron 作业。

    http://guides.rubyonrails.org/action_controller_overview.html#session

    https://github.com/javan/whenever

    【讨论】: