2012-02-20 40 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