使用祖先轨道遍历嵌套子项

时间:2015-01-16 09:44:52

标签: ruby-on-rails tree ancestry

我试图遍历节点的子节点。我已经尝试了一些事情,但我陷入了困境。我调用了一个遍历节点及其所有子节点的函数,但我想将其存储在变量或者集合,所以我可以逐一显示它们。这是功能。

module MessagesHelper
    def nested_comments(comments)
    comments.map do |comment, sub_comments|
       render(comment) + content_tag(:div, sub_comments, :class => "nested_comments")
     end.join.html_safe
   end
end

这是控制器文件

def index
     @messages = Message.all

  end

在index.html.erb中调用此函数。

<div class="Pages index">
   <h2>Messages</h2>

   <%= link_to("Add new Page",{:action => 'new'}, :class => 'action new') %>

      <%= nested_comments @messages.arrange(:order => :likes) %>

          </div> 

这会以此形式显示结果。

{#<Message id: 5, content_title: "2-2", content: "eeer", likes: 67, created_at: "2015-01-15 11:32:38", updated_at: "2015-01-15 11:32:38", ancestry: "1/3">=>{}, #<Message id: 6, content_title: "qqq", content: "asa", likes: 129, created_at: "2015-01-15 11:33:10", updated_at: "2015-01-15 11:33:10", ancestry: "1/3">=>{#<Message id: 7, content_title: "jjjjhjjggjgj", content: "hjhjhjhj", likes: 90, created_at: "2015-01-15 17:01:02", updated_at: "2015-01-15 17:01:02", ancestry: "1/3/6">=>{}}}

现在将所有结果存储在一个集合中我想逐个遍历它们或将它存储在变量中我该怎么做?

0 个答案:

没有答案