我必須在用戶輸入貨件詳細信息後在結賬頁面上顯示總價。我的購物車模型,我用在視圖中顯示的總價類似在Rails中顯示購物車總額,Rails方式
<%= number_to_currency(@cart.total_price) %>
現在我想顯示總計是TOTAL_PRICE +裝運TOTAL_PRICE方法。裝運使用三個參數重量,狀態和提供者計算。假設狀態爲和供應商是恆定的,現在我們只需要擔心重量。所以爲此,我在購物車模型中有一個shipment_rate方法。
def shipment_rate(weight, provider, state)
# calculation code here
end
這是好到這樣的視圖中使用這個方法:
<%- shipment = cart.shipment_rate(weight,'UPS','AK')%>
爲此,我必須提供車中的物品的總重量,以及我可以計算出使用方法@ cart.total_weight。 Rails的做法是什麼?這是好來調用查看這些方法如下:
<%- total = @cart.total_price %>
<%- weight = @cart.total_weight %>
<%- shipment = cart.shipment_rate(weight,'UPS','AK')%>
...
,然後在同一個視圖中使用下面這些值像
<span>Amount: <%= number_to_currency total %></span>
<span>Shipment: <%= number_to_currency shipment %></span>
<span>Total: <%= number_to_currency total + shipment %></span>
只是出於好奇:你爲什麼不使用[施普雷商務部(http://spreecommerce.com) 如果需要例如可以將值傳遞到它? –
@RyanBigg我打算使用[active_shipping](https://github.com/Shopify/active_shipping),但客戶端有一些自定義實現記在 – androidharry
什麼樣的自定義實現?我對他們的想法感興趣。 –