【问题标题】:Rails undefined method `*' for nil:NilClass in viewRails 未定义的方法 `*' for nil:NilClass in view
【发布时间】: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


【解决方案1】:

原来我在发票控制器的显示区域中有@invoice_item = @invoice.invoice_items.new,而不是发票项目创建表单的@invoice_item = InvoiceItem.new。

它 一直在工作:/

谢谢!

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多