<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 07 Sep 2008 22:49:30 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>Custom validation with rails: words with more than 26 characters</title>
      <link>http://snippets.dzone.com/posts/show/5266</link>
      <description>This goes in whatever model you're validating.  "Subject" can be any of the symbols in your model and doesn't need the : in front of it.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validate&lt;br /&gt;	if subject.split.any?{|w| w.length &gt; 26}&lt;br /&gt;		errors.add(:subject, "cannot have words more than 26 consecutive characters")&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 21 Mar 2008 19:40:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5266</guid>
      <author>ioda006 ()</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>
  </channel>
</rss>
