【问题标题】:development works but not in production开发工作但不在生产中
【发布时间】:2016-07-07 16:54:29
【问题描述】:

运行以下代码时出现错误:

ActionView::Template::Error(nil:NilClass 的未定义方法 `total__quantity'):

error :

nil:NilClass 的未定义方法 `total__quantity'

【问题讨论】:

  • 似乎@orgs 不包含您要查找的元素。

标签: html ruby-on-rails haml


【解决方案1】:

你试过了吗?

@org.children.each do |child|
  if (!child.total_quantity.nil?)
    %tr
      %td.child= link_to child.shrt_name, child

我假设从您的原始帖子中,您不小心在 org.children.each 之前省略了 @

我也不确定为什么您觉得需要在循环中重新分配 child 的值。

【讨论】:

  • 他的问题不是childnil,而不是total_quantity 返回nil。它说undefined method ... for NilClass; child` 是NilClass,对吧?
  • 这是问题的一部分。我的问题是为什么 child 从另一个集合中重新分配,因为 org@orgs 明显不同。
  • 是的。我试过。但仍然以错误告终..请您看看我的新帖子,其中包含完整代码并帮助我。
  • child = @organizations[child.id] 这行让我很困惑。 @organizations 是什么?如果是合集,你的意思是@organizations.find(child.id)
  • @organizations 是数据库中的一个表。
【解决方案2】:

这是因为@org 没有孩子。你最好这样做;

children = @org.children

unless children.empty?
   children.each do |child|
      if child.total_quantity > 0
         # Your code here
      end
   end
end

希望这会有所帮助。

【讨论】:

  • 嗨@cabolanoz..你能看到我的新帖子,其中包含我的完整代码并帮助我完成这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 2011-10-29
  • 2020-12-13
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多