【问题标题】:Rails session value being deleted when deleting a line_item删除 line_item 时删除 Rails 会话值
【发布时间】:2012-10-22 11:53:11
【问题描述】:

这个让我很困惑。我还是 Rails 的新手,所以可能很简单。

情况:

  1. 我可以将商品添加到购物车,没问题。一切正常,current_cart 方法在每个请求中解析为同一个购物车。
  2. 但是,一旦我从购物车中删除了一个订单项,它就可以工作了,该订单项被删除了,但是
    变量“session[:cart_id]”变为空,一个新的购物车得到
    下次调用 current_cart 时创建。

我正在使用 Devise,所以我不确定这是否与它有关,或者订单项删除方法可能是级联删除会话或类似的东西。

问题是,谁能帮我理解为什么调用 line_item delete 后会话变量变为空?

我创建了一个购物车应用程序,以及使用 Rails 进行敏捷 Web 开发。首先,这是应用程序控制器检索当前购物车的代码:

  private
  def current_cart 
    Checkout::Cart.find(session[:cart_id])
  rescue ActiveRecord::RecordNotFound 
    cart = Checkout::Cart.create 
    session[:cart_id] = cart.id
    cart
  end

现在是我的订单项控制器的代码

类 Checkout::LineItemsController

# POST /checkout/line_items # POST /checkout/line_items.json def 创建

@cart = current_cart
product = Catalog::Product.find(params[:product_id])
apparel_event_log.info (product.to_json)


@checkout_line_item = @cart.line_items.find_or_initialize_by_sku_id(product.part_number)
new_quantity = @checkout_line_item.quantity || 0
@checkout_line_item.quantity = new_quantity+1
@checkout_line_item.line_item_description = product.name
@checkout_line_item.image = product.primary_image

respond_to do |format|
  if @checkout_line_item.save
    format.html { redirect_to @checkout_line_item.cart, notice: 'Line item was successfully created.' }
    format.json { render json: @checkout_line_item, status: :created, location: @checkout_line_item }
  else
    format.html { render action: "new" }
    format.json { render json: @checkout_line_item.errors, status: :unprocessable_entity }
  end
end   end

# 删除 /checkout/line_items/1 # 删除 /checkout/line_items/1.json def 销毁 Checkout::LineItem.delete(params[:id])

respond_to do |format|
  format.html { redirect_to current_cart, notice: 'Line item removed.' }
  format.json { head :no_content }
end   end end

还有订单项模型的代码

class Checkout::LineItem < ActiveRecord::Base
  before_save :default_values

  attr_accessible :customer_update_date, :inventory_status, :line_item_color, :line_item_description, :line_item_size, :line_item_tagline, :line_item_total, :image, :quantity, :sku_id, :style_id, :tax, :tax_code, :timestamps, :unit_price, :cart, :product
  belongs_to :cart
  belongs_to :product, :class_name => 'Catalog::Product'
  has_one :image, :class_name => 'Assets::Medium'

  def default_values
     Rails.logger.debug { "Entering default values" }
     self.quantity ||= 0
   end

end

【问题讨论】:

  • 您在开始修改书本示例之前是否检查过它是否正常工作?在任务 D 结束时检查所有测试是否仍然通过
  • 这本书还可以,我已经过了这本书的学习阶段,并试图实现购物车的其余部分。这本书并没有真正涵盖我可以看到的删除行项目。

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

首先你应该将逻辑移到模型中:

class Cart << AR
  def add(product_id)
    product = Catalog::Product.find(product_id)

    if item = self.line_items.find_or_initialize_by_sku_id(product.part_number)
      item.quantity = (item.quantity || 0) + 1
      item.line_item_description = product.name
      item.image = product.primary_image
      return item.save
    end
  end
end


class Checkout::LineItemsController < ApplicationController
    # POST /checkout/line_items # POST /checkout/line_items.json def create

    @cart = current_cart
    apparel_event_log.info (product.to_json)

    respond_to do |format|
      if @checkout_line_item = @cart.add(params[:product_id])
        format.html { redirect_to @checkout_line_item.cart, notice: 'Line item was successfully created.' }
        format.json { render json: @checkout_line_item, status: :created, location: @checkout_line_item }
      else
        format.html { render action: "new" }
        format.json { render json: @checkout_line_item.errors, status: :unprocessable_entity }
      end
    end   
end

其次你应该添加开头:

  private
  def current_cart 
    begin
      Checkout::Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound 
      cart = Checkout::Cart.create 
      session[:cart_id] = cart.id
      cart
    end
  end

【讨论】:

  • 感谢我尝试添加开头,但没有成功。稍作更新,但事实证明 line item delete 方法实际上是删除整个会话,而不仅仅是会话中的对象。我检查了 session.inspect,它改变了会话 ID。
【解决方案2】:

好的,我现在确实设法解决了这个问题。太难了!

使用 link_to ... :destroy 或删除似乎是某种问题,我认为默认的 rails 代码没有产生 CSRF 保护,因此使会话无效。

我已经解决了主要部分,将 link_to 替换为 button_to 以删除订单项,这似乎可以正常工作,至少在功能上是这样。

我真的更喜欢使用链接而不是按钮。这似乎是一个更常见的问题,所以我会在论坛中寻找答案,但如果您知道答案,请直接在此线程上发布链接或答案。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2014-11-15
  • 1970-01-01
  • 2015-04-02
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多