0
我在构建新的Rails应用程序时遇到问题。Rails未定义方法'*'为零:NilClass在视图
当试图在视图运行@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和折扣设置为0,每个项目(有3)已经inc_vat为1
有什么问题?
您是否在控制器操作中使用'select'来限制为发票项目返回的字段?这可能会导致您看到它在控制台中正常工作的行为,但在应用程序中失败。 – x1a4 2012-08-07 22:04:50