by the way, I tried this first, and it works, but it''s non recursive:
in the view, where @pages gets all the pages from the controller
--
<ul>
<% @pages.first.children.each do |p| %>
<li><%= p.name %>
<% if p.children.size > 0 %>
<ul>
<% p.children.each do |c| %>
<li><%= c.name %><br/>
<% end %>
</ul>
<% end %>
<% end %>
</ul>
---
(how and where (controller/model/helper?) do I make a recursive
function out of this?)
Gijs Nijholt
On 8/30/06, Gijs Nijholt <gijs.nijholt@gmail.com>
wrote:> Hello Railslist,
>
> I want to draw a simple single-table Acts_as_tree model.
> I''ve read some articles about it and read the API docs, and now
I''ve
> got this running:
>
> in controller I have:
> ---
> def index
> @pages = Page.find(:all)
> end
> ---
> in the model for Page I have ofcourse:
> ---
> acts_as_tree :order => "position"
> ---
> in the helper I have;
> ---
> def list_all_pages(pages)
> if pages.size > 0
> ret = "<ul>\n"
> pages.each do |subpage|
> ret += "<li>\n"
> ret += link_to h(subpage.name)+"
> ("+subpage.children.size.to_s+")", :action =>
''edit'', :id => subpage
> ret += link_to
" "+h("<add>"), :action
=>''new'', :id
> => subpage
> ret += "\n"
> if subpage.children.size > 0
> ret += list_all_pages(subpage.children)
> end
> ret += "</li>\n"
> end
> ret += "</ul>\n"
> end
> end
> ---
> and in the view I have:
> ---
> <h1>Index</h1>
> <%= list_all_pages(@pages) %>
> ---
>
> This gives me a nice tree of all the pages, but since I pass the
> @pages variable to the list_all_pages function, it literally prints
> every page:
> root (4) <add>
> news (2) <add>
> archive (0) <add>
> current (0) <add>
> artists (0) <add>
> bands (0) <add>
> contact (0) <add>
> news (2) <add>
> archive (0) <add>
> current (0) <add>
> artists (0) <add>
> bands (0) <add>
> contact (0) <add>
> archive (0) <add>
> current (0) <add>
>
> While this is what I am trying to get:
> root (4)
> news (2)
> archive (0)
> current (0)
> artists (0)
> bands (0)
> contact (0)
>
> I tried limiting the Page.find(:all), which didnt work.
> How can I prevent the function from repeating the tree?
>
> Thanks in advance,
>
> Gijs Nijholt
>