【发布时间】:2016-07-07 16:54:29
【问题描述】:
运行以下代码时出现错误:
ActionView::Template::Error(nil:NilClass 的未定义方法 `total__quantity'):
error :
nil:NilClass 的未定义方法 `total__quantity'
【问题讨论】:
-
似乎
@orgs不包含您要查找的元素。
标签: html ruby-on-rails haml
运行以下代码时出现错误:
ActionView::Template::Error(nil:NilClass 的未定义方法 `total__quantity'):
error :
nil:NilClass 的未定义方法 `total__quantity'
【问题讨论】:
@orgs 不包含您要查找的元素。
标签: html ruby-on-rails haml
你试过了吗?
@org.children.each do |child|
if (!child.total_quantity.nil?)
%tr
%td.child= link_to child.shrt_name, child
我假设从您的原始帖子中,您不小心在 org.children.each 之前省略了 @
我也不确定为什么您觉得需要在循环中重新分配 child 的值。
【讨论】:
child 是nil,而不是total_quantity 返回nil。它说undefined method ... for NilClass; child` 是NilClass,对吧?
org 和 @orgs 明显不同。
child = @organizations[child.id] 这行让我很困惑。 @organizations 是什么?如果是合集,你的意思是@organizations.find(child.id)?
这是因为@org 没有孩子。你最好这样做;
children = @org.children
unless children.empty?
children.each do |child|
if child.total_quantity > 0
# Your code here
end
end
end
希望这会有所帮助。
【讨论】: