2013-04-05 50 views
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需要返回到拿到3个PARAMS .. render_menu将抓取默认的菜单项,如果没有参数给出。

+0

顺便说一句:代码'<%=儿童%>'将呈现结果'children.to_s'这可能不是你想要的。 – toro2k 2013-04-05 14:55:33

回答

0

您必须使用yield并将替换为collectrender_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> 
+0

完美的作品!谢谢! – 2013-04-06 13:07:19

相关问题