我在运行以下代码时出错:
ActionView::Template::Error (nil:NilClass 的未定义方法 'total__quantity'):
error :
undefined 方法 'total__quantity' for nil:NilClass
你试过吗?
@org.children.each do |child|
if (!child.total_quantity.nil?)
%tr
%td.child= link_to child.shrt_name, child
我假设从您的原始帖子中,您不小心省略了org.children.each
之前的@
我也不确定为什么你觉得有必要在你的循环中重新分配child
的值。
发生这种情况是因为@org
没有孩子。你最好这样做;
children = @org.children
unless children.empty?
children.each do |child|
if child.total_quantity > 0
# Your code here
end
end
end
希望这有帮助。