0

在我看来,我得到了所有页面

我得到了我所有的页面以及所有的孩子和子孩子。如何得到树形结构如下图:

<% for page in @pages%>
<li id="<%= page.id%>_page">    
    <div class="link">    
        #my attributes for the div<
      /div>    
#here I got the all siblings of that page. But here the structure is of just two      
#levels. I need upto n-levels.    
<% @childs = page.*descendants* %>    
<% if !@childs.nil? && !@childs.empty? %>
    <% for child in @childs%>     
    <ol class="child">
        <li id="<%= child.id%>_page">
            <div class="link">
                #my attributes for the div
            </div>
            </li>
    </ol>
    <%end%>
    <%end%>
</li>
<%end%>

我正在使用嵌套集并希望以这种方式构建结构:

page1
     page 2
     page 3
           page 3.1
                   page 3.2
                           page3.4
                                   ...so on to last child
page 4
page 5
....so on to N-levels...
4

2 回答 2

0

您正在尝试在视图中创建递归,这不仅复杂而且会使您的视图代码混乱很多。一种干净的方法是定义一个递归生成输出的辅助函数。

我没有任何具体的例子,但你可以在这个类似问题的答案之一中检查一个

于 2012-07-31T05:53:23.557 回答
0

尝试后..我得到了解决方案...我渲染了部分,而后者又渲染了自己..例如_show_children.html.erb ..

    <% for page in @pages%>
    <li id="<%= page.id%>_page">    
        <div class="link">    
            #my attributes for the div<
          /div>    
    #here I got the all siblings of that page. But here the structure is of just two      
    #levels. I need upto n-levels.    
    <% @childs = page.descendants %>    
    <% if !@childs.nil? && !@childs.empty? %>
        <% for child in @childs%>     
         <%= render :partial => "show_children", :locals  => {:children => child`enter code here`.children  }%>
        <%end%>
        <%end%>
    </li>
<%end%>


.this partial will inturn render itself`
于 2012-08-02T09:50:28.330 回答