acts_as_tree:
http://snippets.dzone.com/tag/acts_as_tree
------------
// This takes a collection of acts_as_tree objects and creates a unordered list.
//
// Put this in your helper:
// ... and this in your view:
http://snippets.dzone.com/tag/acts_as_tree
------------
// This takes a collection of acts_as_tree objects and creates a unordered list.
//
// Put this in your helper:
def tree_ul(acts_as_tree_set, init=true, &block)
if acts_as_tree_set.size > 0
ret = '<ul>'
acts_as_tree_set.collect do |item|
next if item.parent_id && init
ret += '<li>'
ret += yield item
ret += tree_ul(item.children, false, &block) if item.children.size > 0
ret += '</li>'
end
ret += '</ul>'
end
end
// ... and this in your view:
<%= tree_ul(@acts_as_list_collection) {|item| link_to h(item.name), :action => 'edit', :id => item } %>