I am having problems building a new Rails app.
When trying to run @invoice.total in the view, I get this error:
undefined method `*' for nil:NilClass
However, running @invoice.total works in the console. I have tried using HAML and/or ERB with the same problem.
The code which runs for @invoice.total is in the model and is below:
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
The invoice has vat_rate and discount set to 0 and each of the items (there are 3) have inc_vat as 1.
What’s the problem?
Turns out I had
@invoice_item = @invoice.invoice_items.newin the show area of the invoices controller instead of@invoice_item = InvoiceItem.newfor the Invoice Item creation form.It had been working though :/
Thanks!