我有一家小型的铁路店,并且帮助者计算总价和折扣(如果已应用),但我想确定实际折扣显示在视图 -Rails,从2名佣工处获得折扣金额
def total_price
price = 0
order_products.each do |order_products|
# add the item price to total
price += (order_products.price_in_pence * order_products.quantity)
end
apply_discount(price)
end
def apply_discount(price)
return price if discount.nil?
if discount.percentage.present?
price = price - ((price/100) * discount.percentage)
elsif discount.money.present?
price = price - discount.money
end
price < 0 ? 0 : price
end
def discount_amount
I need something here to take the total_price from above
before a discount and then the apply_discount and subtract the 2.
end
'discount_amount'不能像其他两个助手一样写入。您需要解决'total_price'和'apply_discount',因为当前正在调用另一个。你无法得到不同的值。 – meagar 2014-10-07 07:02:05
您正在寻找物品/产品级别或订单级别的折扣金额? – Surya 2014-10-07 07:10:31
另外,尽量避免:'order_products.each do | order_products |'。改为将管道中的变量更改为不同。就像:'order_products.each do | order_product |',这样你就不会与你在块内执行的操作混淆。 – Surya 2014-10-07 07:11:59