0

我想做这个:

<div class="menu">

  <%- render_menu do |title,path,children| %>

    <%= link_to title, path %>

    <div class="submenu">
      <%= render_menu(children) do |title,path,children| %>
        <%= link_to title, path %>
        <%= children %>
      <%- end %>
    </div>

  <% end %>

</div>

该方法render_menu看起来像这样:

def render_menu(children=nil)
  children = Paths.roots if children.nil?
  children.collect do |child|
    [ child.title, child.path, child.children ]
  end
end

我不确定render_menu需要返回什么来获取三个参数。render_menu如果没有给出参数,它将获取默认菜单项。

4

1 回答 1

0

您必须使用yield和替换eachfor collectinside render_menu

def render_menu(children=nil)
  children = Paths.roots if children.nil?
  children.each do |child|
    yield([child.title, child.path, child.children])
  end
end

您还应该修改模板以不显示返回的值render_menu

<div class="submenu">
    <% render_menu(children) do |title,path,children| %>
        <%= link_to title, path %>
        <%= children %>
    <% end %>
</div>
于 2013-04-05T14:44:57.950 回答