为什么我的助手的递归方法没有返回每个值?
我想显示用 gem 祖先管理的类别树。
我想使用一个助手,它将递归地遍历树并一一返回类别,目前没有 html 标签或内容。
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
参数是根类别之一。
它应该返回什么?
- 在网页中: 它只显示根级别类别
Sport Beauty Automobile
- 在控制台中:
Men Indoor Women Children Water sport Garage
如果得到它们,则意味着递归有效,但确实如此不是。为什么它只返回第一次迭代?
另外我想按以下顺序获取它们:
root/child/child-of-child
但如果我想返回category.name,它应该位于最后一个位置。
您能给我您的意见吗?
PS:我刚刚发现(在添加标签期间)我在搜索过程中一直使用“递归”一词,但它并不存在,即使很多人在 stackOveflow 上使用它;o) - > “递归”,但我仍然卡住了
**编辑**
现在我使用这段代码:
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没有被解释,并且相同的代码显示在显示的网页中。我的意思是我发现
我可能错过了一些东西......也许是知识oO
Thx
I want to display a tree of categories managed with the gem ancestry.
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
The category
argument is one of the root categories.
What should it return?
- In the web page:
It displays only the root level categorySport 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
but if I want to return category.name
, it should be in the last position.
Could you please give me your comments?
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
** EDIT **
Now I use this code:
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
which gives me:
<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>
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的方法将仅返回一个值(实际上是第一次调用category.name)
关于控制台,您将获得循环内的 put(这不是方法的返回值)。
尝试一下,如果还有什么地方不够清楚,请告诉我:
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:
我回答我的最后一个编辑问题。我必须添加这一行:
来解释该字符串。
谢谢
I answer for my last edit question. I had to add this line :
to interprete the string.
Thx