0

act-as-shopping-cart gemを使用しているときに、次のエラーが発生します。

undefined method `*' for nil:NilClass  Extracted source (around line #5):

2:
3: <%= render :partial => 'shopping_cart_item', :collection => @shopping_cart.shopping_cart_items %>
4:
5:  SubTotal:<%= number_to_currency @shopping_cart.subtotal %>
6:  Taxes:<%= number_to_currency @shopping_cart.taxes %>
7:  Total:<%= number_to_currency @shopping_cart.total %>

フィールドは gem ソースで定義されているようです。

show.html.erb

<h1>Shopping Cart</h1>

<%= render :partial => 'shopping_cart_item', :collection => @shopping_cart.shopping_cart_items %>

<div><b>SubTotal:</b><%= number_to_currency @shopping_cart.subtotal %></div>
<div><b>Taxes:</b><%= number_to_currency @shopping_cart.taxes %></div>
<div><b>Total:</b><%= number_to_currency @shopping_cart.total %></div>

shopping_cart.rb

class ShoppingCart < ActiveRecord::Base
  acts_as_shopping_cart

  def tax_pct
    8.25
  end

  def taxes
    (subtotal - 10) * tax_pct
  end

end

アプリケーション トレース

app/views/shopping_carts/show.html.erb:5:in `_app_views_shopping_carts_show_html_erb__2338506009803118188_70153042369260'

shopping_carts.controller

class ShoppingCartsController < ApplicationController
  before_filter :extract_shopping_cart

  def create
    @product = Product.find(params[:product_id])
    @shopping_cart.add(@product, @product.price)
    redirect_to shopping_cart_path
  end

  def show

  end

  private
  def extract_shopping_cart
    shopping_cart_id = session[:shopping_cart_id]
    @shopping_cart = session[:shopping_cart_id] ? ShoppingCart.find(shopping_cart_id) : ShoppingCart.create
    session[:shopping_cart_id] = @shopping_cart.id
  end
end

私のレポ

https://github.com/atbyrd/Bootstrapped_Devise

4

1 に答える 1

2

この問題は、価格設定されていないアイテムをショッピング カートに追加した場合にのみ発生します。

したがって、すべての製品に価格があることを確認する必要があります。おそらく検証を使用し、おそらく before_save 関数を使用しますが、すべての製品に価格があることを確認すると、この問題は解消されます。

psこれをテストする前に、価格のない製品をデータベースから削除することを忘れないでください

于 2012-12-27T05:03:05.050 に答える