【问题标题】:Why does my recursive method from helper not return every value?为什么我的帮助程序的递归方法不返回每个值?
【发布时间】:2011-08-29 19:08:24
【问题描述】:

我想显示一个由 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,它应该在最后一个位置。

你能给我你的cmets吗?

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

谢谢

【问题讨论】:

  • 请添加您用于在模板上呈现它的视图代码。您可能缺少包含 display_tree() 方法的 raw() 方法,以防止 Rails >= 3 自动转义 html。

标签: ruby-on-rails ruby methods recursion helper


【解决方案1】:

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

试试这个,如果还有什么不清楚的地方告诉我:

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

【讨论】:

  • 您可能需要更具体的格式。如果您可以提供有关预期返回值的更详细信息,我们可以改进示例。
  • 这是对的;嵌套调用的 puts 不直接附加到根的输出缓冲区。我认为这是一种非常聪明的处理方式。
  • 谢谢 :) 那是我的误会!!
  • 我编辑了我的问题以获得有关格式化结果的更多帮助。请感到邀请看看:)
【解决方案2】:
        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

解释字符串。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2021-05-12
    • 2022-11-10
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多