新しい Rails アプリの作成に問題があります。
@invoice.total
ビューで実行しようとすると、次のエラーが発生します。
undefined method `*' for nil:NilClass
ただし、実行@invoice.total
はコンソールで機能します。同じ問題で HAML や ERB を使用してみました。
実行されるコード@invoice.total
はモデル内にあり、以下のとおりです。
def items_total
items_total = 0
self.invoice_items.each do |i|
items_total += i.price * i.quantity
end
items_total
end
# instead of copying this code all of the time
def vat_calc
(1 + self.vat_rate / 100)
end
def discount_calc
(1 - self.discount / 100)
end
# total times to add vat on top and remove discount
def total
items_total * discount_calc * vat_calc
end
請求書の vat_rate と discount は 0 に設定されており、各項目 (3 つある) の inc_vat は 1 に設定されています。
どうしたの?