未定义的方法`+'为零:NilClass狂欢

时间:2016-04-19 07:00:57

标签: ruby-on-rails spree

我正在运行一个狂欢应用。

当我尝试在购物车中添加任何产品时,我收到以下错误。

undefined method `+' for nil:NilClass

只有在我添加同一产品的option typesvariants时才会出现此错误。

我不确定这里到底出了什么问题,因为我没有对代码做任何改动。

这是它显示的提取源。

   if quantity.between?(1, 2_147_483_647)
    begin
      order.contents.add(variant, quantity, options)
    rescue ActiveRecord::RecordInvalid => e
      error = e.record.errors.full_messages.join(", ")
    end

这是我的订单管理员的populate功能。

# Adds a new item to the order (creating a new order if none already exists)
def populate
  order    = current_order(create_order_if_necessary: true)
  variant  = Spree::Variant.find(params[:variant_id])
  quantity = params[:quantity].to_i
  options  = params[:options] || {}

  # 2,147,483,647 is crazy. See issue #2695.
  if quantity.between?(1, 2_147_483_647)
    begin
      order.contents.add(variant, quantity, options)
    rescue ActiveRecord::RecordInvalid => e
      error = e.record.errors.full_messages.join(", ")
    end
  else
    error = Spree.t(:please_enter_reasonable_quantity)
  end

  if error
    flash[:error] = error
    redirect_back_or_default(spree.root_path)
  else
    respond_with(order) do |format|
      format.html { redirect_to cart_path }
    end
  end
end

请帮帮我。

2 个答案:

答案 0 :(得分:1)

在将它们发送到狂欢之前,您需要确保variantquantityoptions的值。

您收到此错误的事实可能会被视为他们身边的错误,因为您预计会有一条很好的错误消息说" variant是nil"等等。

要解决您的问题,我会在将这些值发送到狂欢之前检查这些值是否有效。

答案 1 :(得分:0)

有关此问题的未来观点。

检查Variant cost_currency属性是否与Spree中配置的相同。您可以在rails控制台中进行检查:

Spree::Config.get(:currency)

有时,当默认情况下使用某种货币初始化spree然后更改默认货币时会发生这种情况。