为什么我的助手的递归方法没有返回每个值?

发布于 2024-11-08 17:26:06 字数 2149 浏览 0 评论 0原文

我想显示用 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 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-11-15 17:26:06

您使用的方法将仅返回一个值(实际上是第一次调用category.name)
关于控制台,您将获得循环内的 put(这不是方法的返回值)。

尝试一下,如果还有什么地方不够清楚,请告诉我:

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

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
我不吻晚风 2024-11-15 17:26:06
        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>"
            tree.html_safe #That was missing to interprete the html returned...
          end
        end

我回答我的最后一个编辑问题。我必须添加这一行:

tree.html_safe

来解释该字符串。

谢谢

        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>"
            tree.html_safe #That was missing to interprete the html returned...
          end
        end

I answer for my last edit question. I had to add this line :

tree.html_safe

to interprete the string.

Thx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文