2013-04-05 51 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

相關問題