0

カートに入っているすべての商品の合計金額を計算しようとしています。しかし、私はundefined method total_cart_price for ActiveRecord::Relation:xxx

ショップカートモデル (shop_cart.rb)

has_many :shop_cart_items, :dependent => :destroy


  def total_cart_price
    shop_cart_items.to_a.sum { |item| item.total_price }
  end

ショッピングカートアイテムモデル (shop_cart_item.rb)

attr_accessible :quantity, :shop_cart_id, :shop_product_id

  belongs_to :shop_product
  belongs_to :shop_cart


  def total_price
    shop_product.sell * quantity
  end

バスケット ビュー (shop/basket.html.erb)

<tbody>
    <% @cart_items.each do |item| %>
        <tr>
        <td><%= item.shop_product.name %></td>
        <td><%= number_to_currency(item.shop_product.sell) %></td>
        <td><%= item.quantity %></td>
        <td><%= number_to_currency(item.total_price) %></td>
        </tr>
    <% end %>
    <tr>
    <td></td>
    <td></td>
    <td>Subtotal:</td>
    <td><%= number_to_currency(@cart_items.total_cart_price) %></td>
    </tr>
</tbody>

単一のアイテムの合計価格は正常に機能します。エラーは、すべてのアイテムの合計を計算しようとしています。

shop#basket ビューからメソッドを呼び出しているため、shop_cart コントローラーまたはビューがありません。これが問題の原因であるかどうかわかりません。

どんな助けでも大歓迎です。

4

1 に答える 1

0

試してみてください @cart_items.shop_cart.total_cart_price、カート方式です。

于 2012-11-02T11:02:16.323 に答える