我的名为stocks的表包含product_id, color_id, storage_id and in_stock列.
对于给定的产品,我想按存储对所有库存进行分组,然后对于每个存储,我想要显示产品(颜色):in_stock
我该如何编写方法以及如何渲染?
这是一个经典问题,但无法找到最佳方法.我有一个id为project_billing_code_id的下拉列表和3个值(1,2,3).
如果选择值= 1,则显示id为1且仅为此的div.div二和三必须隐藏.我还希望在加载视图时实现这一点,而不仅仅是在更改时.
$(document).ready(function() {
$("#hourly").hide();
$("#per_diem").hide();
$("#fixed").hide();
$("#project_billing_code_id").change(function() {
if ($("#project_billing_code_id").val() == '1') {
$("#hourly").show();
}
else if ($("#project_billing_code_id").val() == '2') {
$("#per_diem").show();
}
else if ($("#project_billing_code_id").val() == '3') {
$("#fixed").show();
}
else {
$("#hourly").hide();
$("#per_diem").hide();
$("#fixed").hide();
}
});
});
Run Code Online (Sandbox Code Playgroud) 上下文:我有2个模型Order和Item.
我想根据item.quantity*item.price计算Item小计.现在这在视图中完成(但不是在适当的地方).
<%= number_to_currency(item.quantity * item.price) %>
Run Code Online (Sandbox Code Playgroud)
我还需要计算订单总数但我被卡住了.我没有任何专栏.什么是最好的方法?使用该型号?帮助者或观察者?
现在我设法通过Order helper进行小计工作
def item_subtotal(item)
item_subtotal = item.quantity * item.price
end
Run Code Online (Sandbox Code Playgroud)
工作方案:
物品模型
def subtotal
price * quantity
end
Run Code Online (Sandbox Code Playgroud)
在视图渲染中 <%= item.subtotal %>
订单模型
def total_price
total_price = items.inject(0) { |sum, p| sum + p.subtotal }
end
Run Code Online (Sandbox Code Playgroud)
订单#show view render <%= number_to_currency(@order.total_price) %>