【发布时间】:2012-08-07 21:57:35
【问题描述】:
我在构建新的 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
发票的增值税率和折扣设置为 0,每个项目(有 3 个)的 inc_vat 为 1。
有什么问题?
【问题讨论】:
-
您是否在某个控制器操作中使用
select来限制为您的发票项目返回的字段?这可能会导致您看到它在控制台中正常工作但在应用程序中失败的行为。
标签: ruby-on-rails ruby-on-rails-3 nomethoderror