2012-02-20 78 views
1

嗨,大家好即時通訊玩回報率1.9 我被困在零不能被強迫的BigDecimal

零不能被強迫的BigDecimal

軌道3紅寶石的環境錯誤

我需要得到車 我知道問題出在哪裏(我認爲),但我幾乎沒有每一件事情裏面的產品的總成本

車/ show.html.rb

<div class="cart_title" >Your Cart</div> 
    <table> 
     <% for item in @cart.line_items %> 
    <tr> 
     <td><%= item.quantity %>&times;</td> 
     <td><%= item.product.title %></td> 
    <td class="item_price" ><%= number_to_currency(item.total_price) %></td> 
    </tr> 
    <% end %> 
     <tr class="total_line" > 
     <td colspan="2" >Total</td> 
     <td class="total_cell" ><%= number_to_currency(@cart.total_price) %></td> 
    </tr> 
    </table> 
     <%= button_to 'Empty cart', @cart, :method => :delete, 
     :confirm => 'Are you sure?' %> 

模型/ line_item.rb

def total_price 
    line_items.to_a.sum { |item| item.total_price } 
    end 

模型/ cart.rb

def total_price 
    product.price * quantity 
    end 

我的第二個選擇是

def total_price 
    if product.price 
     product.price * quantity 
    else 
     product.price = "0.0".to_d 
    end 
    end 

但仍然t他不會工作

感謝我們更多的力量!

回答

0

的問題是在你的cart.rb型號:

def total_price 
    product.price * quantity 
end 

您沒有在您的購物車單品;您的購物車中已有許多產品。您需要將購物車中所有訂單項的價格總和。 (您希望使用訂單項,而不是直接使用產品,以防在客戶承諾以舊價格購買產品後產品價格發生變化。)

如何解決此問題取決於你的模型,但是,你要尋找的是這樣的:

def total_price 
    line_items.to_a.each(&:total_price).sum 
end 

這將在您的收藏line_items上運行的每個項目的total_price方法,構建所有的列表,然後sum列表。

+0

我該怎麼做?你可以給我一個例子即時通訊新的感謝:) – Led 2012-02-21 00:07:51