<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: acts_as_tree code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 14:49:22 GMT</pubDate>
    <description>DZone Snippets: acts_as_tree code</description>
    <item>
      <title>Rails UL Helper for acts_as_tree</title>
      <link>http://snippets.dzone.com/posts/show/4388</link>
      <description>// This takes a collection of acts_as_tree objects and creates a unordered list.&lt;br /&gt;// &lt;br /&gt;// Put this in your helper:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def tree_ul(acts_as_tree_set, init=true, &amp;block)&lt;br /&gt;    if acts_as_tree_set.size &gt; 0&lt;br /&gt;      ret = '&lt;ul&gt;'&lt;br /&gt;      acts_as_tree_set.collect do |item|&lt;br /&gt;        next if item.parent_id &amp;&amp; init&lt;br /&gt;        ret += '&lt;li&gt;'&lt;br /&gt;        ret += yield item&lt;br /&gt;        ret += tree_ul(item.children, false, &amp;block) if item.children.size &gt; 0&lt;br /&gt;        ret += '&lt;/li&gt;'&lt;br /&gt;      end&lt;br /&gt;      ret += '&lt;/ul&gt;'&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// ... and this in your view:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  &lt;%= tree_ul(@acts_as_list_collection) {|item| link_to h(item.name), :action =&gt; 'edit', :id =&gt; item } %&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 04 Aug 2007 17:46:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4388</guid>
      <author>goodieboy (Matt Mitchell)</author>
    </item>
    <item>
      <title>Acts As Tree Category Display</title>
      <link>http://snippets.dzone.com/posts/show/1919</link>
      <description>You should have a categories table with a parent_id field.  Root categories should have the parent_id of 0.&lt;br /&gt;&lt;br /&gt;I modified this post:&lt;br /&gt;http://www.bigbold.com/snippets/posts/show/296&lt;br /&gt;&lt;br /&gt;because the first one worked if you did stuff in your view to make the root categories come up, but I agree with the commenter that things should be contained to the helper - however I've managed to break the DRY rule in doing so.  If anyone has any suggestions for a better way around this I'd love to hear it, so please comment.  So anyway.. this should list your root categories ONLY and NOT your other sub-cats as well so that you have a very pretty little tree.&lt;br /&gt;&lt;br /&gt;Slap this in your view:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%= display_categories(@categories) %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And put this in a helper file.  You'll need it in the application helper most likely since you'll want to call it from multiple views.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;      def display_categories(categories)&lt;br /&gt;	    	   ret = "&lt;ul&gt;"&lt;br /&gt;	   for category in categories&lt;br /&gt;		   if category.parent_id == 0&lt;br /&gt;			ret += "&lt;li&gt;"&lt;br /&gt;			ret += link_to category.name&lt;br /&gt;			ret += find_all_subcategories(category)&lt;br /&gt;			ret += "&lt;/li&gt;"&lt;br /&gt;		   end&lt;br /&gt;		   &lt;br /&gt;	   end&lt;br /&gt;	   ret += "&lt;/ul&gt;"&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;   def find_all_subcategories(category)&lt;br /&gt;    if category.children.size &gt; 0&lt;br /&gt;      ret = '&lt;ul&gt;'&lt;br /&gt;      category.children.each { |subcat| &lt;br /&gt;        if subcat.children.size &gt; 0&lt;br /&gt;          ret += '&lt;li&gt;'&lt;br /&gt;          ret += link_to h(subcat.name), :action =&gt; 'edit', :id =&gt; subcat&lt;br /&gt;          ret += find_all_subcategories(subcat)&lt;br /&gt;          ret += '&lt;/li&gt;'&lt;br /&gt;        else&lt;br /&gt;          ret += '&lt;li&gt;'&lt;br /&gt;          ret += link_to h(subcat.name), :action =&gt; 'edit', :id =&gt; subcat &lt;br /&gt;          ret += '&lt;/li&gt;'&lt;br /&gt;        end&lt;br /&gt;        }&lt;br /&gt;      ret += '&lt;/ul&gt;'&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 16 Apr 2006 15:32:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1919</guid>
      <author>ioda006 ()</author>
    </item>
    <item>
      <title>acts_as_tree unordered list printout</title>
      <link>http://snippets.dzone.com/posts/show/296</link>
      <description>In your view:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;ul&gt;  &lt;br /&gt;&lt;% for category in @categories %&gt;&lt;br /&gt;    &lt;li&gt;&lt;%= link_to h(category.title), :action =&gt; 'edit', :id =&gt; category %&gt;&lt;br /&gt;        &lt;%= find_all_subcategories(category) %&gt;&lt;br /&gt;   &lt;/li&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Place this in a helper:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def find_all_subcategories(category)&lt;br /&gt;    if category.children.size &gt; 0&lt;br /&gt;      ret = '&lt;ul&gt;'&lt;br /&gt;      category.children.each { |subcat| &lt;br /&gt;        if subcat.children.size &gt; 0&lt;br /&gt;          ret += '&lt;li&gt;'&lt;br /&gt;          ret += link_to h(subcat.title), :action =&gt; 'edit', :id =&gt; subcat&lt;br /&gt;          ret += find_all_subcategories(subcat)&lt;br /&gt;          ret += '&lt;/li&gt;'&lt;br /&gt;        else&lt;br /&gt;          ret += '&lt;li&gt;'&lt;br /&gt;          ret += link_to h(subcat.title), :action =&gt; 'edit', :id =&gt; subcat &lt;br /&gt;          ret += '&lt;/li&gt;'&lt;br /&gt;        end&lt;br /&gt;        }&lt;br /&gt;      ret += '&lt;/ul&gt;'&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 18 May 2005 14:54:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/296</guid>
      <author>Caged (Justin Palmer)</author>
    </item>
  </channel>
</rss>
