I couldnt find a tutorial anywhere on this so I decided to write one ...I did find a small code snippet on the web - but I thought it would be useful to include something on the Wiki

Suppose you have a tree structure and you want to render a drop down list so the user can choose an option e.g.

<pre><code><%= render_tree_select(@pages, "page_title") %></pre></code>

Assuming a database structure of

<pre><code>id (int)
parent_id (int)
page_title (string)
</pre></code>

Next, you need to create 2 functions 1 for the helper function and 1 for the recursive function which will fill the tree

<pre><code>def render_tree_select(pages, name)
  ret = ''
  ret += "<select>"
  for page in pages 
    ret += "<option>"
    ret += page[name] if page[name]
    ret += recurse_tree(page, 0, name) if page.children.size>0 
  end 
  ret += "</select>"
end
</pre></code>

Next, lets write our recursive function (portions of this were taken from "This site":http://www.bigbold.com/snippets/tags/acts_as_tree 

<pre><code>def recurse_tree(page, depth, name)
		depth = depth + 1
		level = "- " * depth
		ret = ''
		if page.children.size > 0
		  page.children.each { |subpage| 
			if subpage.children.size > 0
			  ret += '<option id="'+subpage.id.to_s+'">'
			  ret += h(level + subpage[name])
			  ret += recurse_tree(subpage, depth)
			  ret += '</option>'
			else
			  ret += '<option id="'+subpage.id.to_s+'">'
			  ret += h(level + subpage[name])
			  ret += '</option>'
			end
			}
		  ret += ''
		end
end
</pre></code>

simply drop in this code into a lib file or your <controller>_helper.rb and in the view simply add

<pre><code><%= render_tree_select(@pages, "page_title") %>
</pre></code>

You should get a drop down list with the sub-levels indented - simple
