我使用简单的导航和内置的嵌套类别构建了导航菜单,我在此菜单中显示。
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
问题是:如何在菜单中显示所有级别的类别。该代码仅使用两级嵌套。
提前感谢
答案 0 :(得分:2)
请看一下refinery-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>
答案 1 :(得分:0)
如果这有帮助,你将不得不使用递归,你可以在这里看到一个例子:Recursive Rails Nested Resources