为什么我的助手中的递归方法不能返回每个值? [英] Why does my recursive method from helper not return every value?
问题描述
我想显示一棵由宝石祖先管理的类别树.
I want to display a tree of categories managed with the gem ancestry.
我想使用一个帮助器,该帮助器将递归地遍历树,并在没有html标签或内容的情况下,逐一返回类别.
I would like to use a helper which will recursively go through the tree and return the categories one by one, for the moment without html tags or content.
module CategoriesHelper
def display_tree(category)
if category.has_children?
category.children.each do |sub_category|
display_tree(sub_category)
puts(sub_category.name) # to check if it goes here
end
end
category.name
end
end
category
参数是根类别之一.
The category
argument is one of the root categories.
它应该返回什么?
- 在网页中:
它仅显示根级别类别
Sport Beauty Automobile
- 在控制台中:
Men Indoor Women Children Water sport Garage
- In the web page:
It displays only the root level category
Sport Beauty Automobile
- In the console:
Men Indoor Women Children Water sport Garage
如果获得它们,则表示递归有效,但无效.为什么只返回第一次迭代?
If get them, then it means that the recursion works, but it does not. Why does it return only the first iteration?
我也想按以下顺序获得它们:
Also I would like to get them in the following order:
root/child/child-of-child
但是如果我想返回category.name
,它应该在最后一个位置.
but if I want to return category.name
, it should be in the last position.
能给我您的意见吗?
PS:我刚刚发现(在添加标签期间)我在搜索过程中一直使用递归"一词,但是它不存在,即使很多人在stackOveflow上使用它也是如此; o)->递归",但仍然被卡住
PS: I just found out (during adding tags) that I was using the word "recursivity" all along my searches but it doesn't exist, even if many people are using it on stackOveflow ;o) -> "recursion", but still I'm stuck
**编辑**
现在我使用此代码:
module CategoriesHelper
def display_tree(category)
tree = "<div class =\"nested_category\">#{category.name}"
if category.has_children?
category.children.each do |sub_category|
tree += "#{display_tree(sub_category)}"
end
end
tree += "</div>"
end
end
这给了我
<div class ="nested_category">Sport
<div class ="nested_category">Men</div>
<div class ="nested_category">Women
<div class ="nested_category">Indoor</div>
</div>
<div class ="nested_category">Children</div>
<div class ="nested_category">Water sport</div>
</div>
<div class ="nested_category">Beauty</div>
<div class ="nested_category">Automobile
<div class ="nested_category">Garage</div>
</div>
但是该html不会被解释,并且在显示的网页中会显示相同的代码.我的意思是我看到
But that html is not interpreted and the same code is shown in the displayed webpage. I mean that I see
我可能错过了一些东西……也许是知识
I probably missed something... maybe knowledge oO
Thx
推荐答案
您使用的方法将仅返回一个值(实际上是对category.name的首次调用) 关于控制台,您将获得循环内的卖权(不是方法的返回值).
The mothod you are using will return just one value (the fist call to category.name actually) About the console, you are getting the puts that you have inside the loop (that is not the return value of the method).
尝试一下,让我知道是否还有一些不清楚的地方:
Try this and let me know if there's still something not clear enough:
module CategoriesHelper
def display_tree(category)
tree = category.name
if category.has_children?
category.children.each do |sub_category|
tree += "/#{display_tree(sub_category)}"
end
end
tree
end
end
这篇关于为什么我的助手中的递归方法不能返回每个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!