2011-03-31 72 views
0

我使用簡單的導航構建了導航菜單,並構建了嵌套的類別,我在此菜單中顯示。Rails簡單導航,嵌套集

navigation.rb代碼:

navigation.items do |primary| 
    Category.roots.map do |root| 
     primary.item ":root_#{root.id}", root.title, category_path(root) do |secondary| 
     root.descendants.map do |desc| 
      secondary.item ":desc_#{desc.id}", desc.title, category_path(desc) 
     end 
     end 
    end 
    end 

的問題是:我怎麼能在我的菜單顯示類別中的所有級別。該代碼只用於兩層嵌套。

預先感謝

回答

2

請看看煉油廠CMS是如何做到這一點。

_menu.html.erb接近你所擁有的。 除此之外,它還有一個名爲_menu_branch.html.erb的部分遞歸地呈現菜單的子菜單。

https://github.com/resolve/refinerycms/blob/master/core/app/views/refinery/_menu.html.erb https://github.com/resolve/refinerycms/blob/master/core/app/views/refinery/_menu_branch.html.erb

代碼切斷從GitHub:

_menu。 html.erb

<nav id='<%= dom_id %>' class='<%= css %>'> 
    <ul> 
    <%= render :partial => '/refinery/menu_branch', :collection => roots, 
       :locals => { 
       :hide_children => hide_children, 
       :sibling_count => (roots.length - 1), 
       :apply_css => true #if you don't care about class='first' class='last' or class='selected' set apply_css to false for speed. 
       } -%> 
    </ul> 
</nav> 

_menu_branch.html.erb

<li<%= ['', css].compact.join(' ').gsub(/\ *$/, '').html_safe %>> 
<%= link_to(menu_branch.title, main_app.url_for(menu_branch.url)) -%> 
    <% if (children = menu_branch.children unless hide_children).present? -%> 
    <ul class='clearfix'> 
     <%= render :partial => '/refinery/menu_branch', :collection => children, 
       :locals => { 
        :apply_css => local_assigns[:apply_css], 
        :hide_children => !!hide_children 
       } -%> 
    </ul> 
    <% end -%> 
</li>