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

ビュー内に再帰を作成しようとしていますが、これは複雑であるだけでなく、ビューコードが非常に乱雑になります。クリーンな方法は、出力を再帰的に生成するヘルパー関数を定義することです。

具体的な例はありませんが、この同様の質問の回答の1つで確認できます。

于 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 に答える