Topic: acts as tree simple menu
This is my first very simple tutorial how to make simple menu using acts as tree.
Database table must have id, name and parent_id columns.
#category model
acts_as_tree :order => "name"
#controller
@categories = Category.find:all
#view
<ul>
<% for category in Category.find_all_by_parent_id(nil) %>
<li>
<%= link_to h(category.name), :controller => 'categories', :action => 'show', :id => category.id %>
<% if Category.find_all_by_parent_id(category.id).empty? %>
<span>no subcategories</span>
<% else %>
<ul><% for category in Category.find_all_by_parent_id(category.id) %>
<li>
<%= link_to h(category.name), :controller => 'categories', :action => 'show', :id => category.id %>
</li>
<% end %>
</ul>
<% end %>
</li>
<% end %>
</ul>
And thats all. Maybe it will be usefull for someone.
Last edited by zattrix (2006-12-05 19:33:54)