<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>I.NFECTIO.US: Enhancing Rails Errors</title>
    <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>"Nothing in life is worth doing if you have no life while doing it"</description>
    <item>
      <title>Enhancing Rails Errors</title>
      <description>After the first day of playing with the Rails framework, I decided the error messages generated by the validation routines were a bit lacking for my taste. It would be nice if the errors displayed followed the same order as your form elements and the humanizing only works if you name your table columns with this in mind. Knowing Ruby and Rails are all-powerful, I figured there must be an elegant way to fix this without writing my own custom error handling for each view. Rails Plugins to the rescue.
&lt;br /&gt;&lt;br /&gt;


&lt;p&gt;So here is my "improved" error_messages_for function.&lt;/p&gt;

&lt;code&gt;&amp;lt;%= error_messages_for 'client', { :priority =&amp;gt; ['first_name', 'last_name'], :attr_names =&amp;gt; {'first_name' =&amp;gt; 'Surely, your mother gave you a name?', 'last_name' =&amp;gt; 'Suffering from Alzheimers are you?', 'gender' =&amp;gt; 'Now I understand why your mother did not name you'}, :defaults=&amp;gt; false, :header =&amp;gt; 'You need to correct {count} error(s) before we can save this {object}', :sub_header =&amp;gt; 'What on earth were you thinking?' } %&amp;gt;&lt;/code&gt;
&lt;p style="margin-top:15px;"&gt;
This will print out:&lt;/p&gt;
&lt;img src="/images/plugins/error_messages_for/error_messages_for.gif" /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;I admit, this is a bit cumbersome, but it solved the problem I had and for anyone else thats anal about little things like this, it will solve your problems too!&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;You can download the plugin &lt;a href="http://www.railtie.net/plugins/error_messages_for-0.2.zip"&gt;here&lt;/a&gt;. To use the plugin, just extract it to your vendor/plugins directory in your Rails Project. View the README and add some spice to your errors. Let me know if you have ideas for improvement or you have any problems. Enjoy!&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
&lt;strong&gt;UPDATE:&lt;/strong&gt; Just released version 0.2. This adds the ability to pass in an array of object names and have the errors consolidated into one container.&lt;/p&gt;</description>
      <pubDate>Thu, 26 Jan 2006 17:52:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:65b5ca53edb6788b05b05531832b1f27</guid>
      <author>Bob Silva</author>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors</link>
      <category>Ruby on Rails</category>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Mike</title>
      <description>Matt Allen: Your fix didn't quite work for me until I modified it to make sure the variable isn't nil before checking that *.error.nil?:

&lt;pre&gt;object_names.each { |name| objects  instance_variable_get("@#{name}") if !instance_variable_get("@#{name}").nil? &amp;&amp; !instance_variable_get("@#{name}").errors.nil? }&lt;/pre&gt;</description>
      <pubDate>Thu, 15 Feb 2007 14:04:29 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:34bef73a-38a4-4a04-9d51-e65bf2c78a19</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-261</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Hernan</title>
      <description>I couldn't make it work for Rails 1.2.1; someone else has the same problem?

Thanks in advance!</description>
      <pubDate>Mon, 12 Feb 2007 13:51:29 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:096a70af-844b-47ac-84b2-631abd07c820</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-260</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by FiFouille</title>
      <description>and I made a mistake ...
true must be replaced by 
&lt;pre&gt;value[1] =~ /^\^/&lt;/pre&gt;

giving this line : &lt;pre&gt;content_tag("ul", errors.collect { |value| content_tag("li", ((options[:attr_names].has_key?(value[0])) ? "#{options[:attr_names][value[0]]}" + (options[:defaults] ? " #{value[1]}" : '') : value[1] =~ /^\^/ ? "#{value[1][1..-1]}" : "#{value[0].humanize} #{value[1]}")) }),&lt;/pre&gt;

replacing the 'old' one :
&lt;pre&gt;content_tag("ul", errors.collect { |value| content_tag("li", ((options[:attr_names].has_key?(value[0])) ? "#{options[:attr_names][value[0]]}" + (options[:defaults] ? " #{value[1]}" : '') : "#{value[0].humanize} #{value[1]}")) }),&lt;/pre&gt;</description>
      <pubDate>Wed, 07 Feb 2007 05:43:31 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d8fb3ab7-3823-4394-ab0a-2199f83f2ccb</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-259</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by FiFouille</title>
      <description>Just made a little modification to imitate comportement of this plugin &lt;a href="http://rubyforge.org/projects/custom-err-msg/" rel="nofollow"&gt;http://rubyforge.org/projects/custom-err-msg/&lt;/a&gt;

So if your validation message starts with a '^', the humanized column name is not display before your custom message

You'll only have to specify order, header and sub_header in your view, errors messages are still held in the model

in error_messages_for.rb

changed : 
content_tag("ul", errors.collect { |value| content_tag("li", ((options[:attr_names].has_key?(value[0])) ? "#{options[:attr_names][value[0]]}" + (options[:defaults] ? " #{value[1]}" : '') : "#{value[0].humanize} #{value[1]}")) }),--

to :
content_tag("ul", errors.collect { |value| content_tag("li", ((options[:attr_names].has_key?(value[0])) ? "#{options[:attr_names][value[0]]}" + (options[:defaults] ? " #{value[1]}" : '') : true ? "#{value[1][1..-1]}" : "#{value[0].humanize} #{value[1]}")) }),

this is my first 'patch' in the source of Rails and Rails plugins, so don't take this as 100% bullet proof

PS : I can't paste the diff file, impossible to get proper form with this commenty box</description>
      <pubDate>Wed, 07 Feb 2007 05:23:08 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:e8241c5b-705f-4a0d-8016-1cd9dcc1e6c5</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-258</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Alexey</title>
      <description>Exxxellent pluging !</description>
      <pubDate>Wed, 31 Jan 2007 07:28:55 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d934331b-2bd1-4506-b71e-3366745868e5</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-257</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Spidou</title>
      <description>try restart the server!</description>
      <pubDate>Sat, 27 Jan 2007 11:11:20 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:7aa523e2-e528-4349-9010-7ac612a90433</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-255</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by J&#252;rgen</title>
      <description>I don't know why, but the plugin is not picked up automatically, I dropped the plugin correctly into the vendor directory. So when I call the error_messages_for method it will still call the standard rails one. Anyone can help?</description>
      <pubDate>Wed, 10 Jan 2007 06:48:11 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d6fc96cc-d6a9-452b-b92d-87ebeefb5f81</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-245</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by dodah</title>
      <description> {'guest' = 'G&#228;st', 'title' = 'Titel', 'comment' = 'Kommentar'}


would be nice if we could do:

 {:guest = 'G&#228;st', :title = 'Titel', :comment = 'Kommentar'}</description>
      <pubDate>Wed, 27 Dec 2006 11:10:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:de39a34a-5dee-4646-b348-d9b578048a14</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-241</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Diego</title>
      <description>Thanks, saved my day. It seems this is a weak point in ruby. Thanks to your help I found a solution</description>
      <pubDate>Fri, 22 Dec 2006 15:06:39 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:c225d5aa-0843-4239-89c9-b6f947726d72</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-239</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by piter</title>
      <description>Could you add to your plugin a small override of class Base from ActionView module? There is hardcoded "div" usage, but i don't think that "div" is the best tag in that case. I think "span" will be better.</description>
      <pubDate>Sun, 03 Dec 2006 15:31:18 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:f881cd53-9dfa-4dfd-a260-da0bc70a3ac4</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-230</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by jdub</title>
      <description>This works great!  Thank you!

A question:

Is there an elegant way to handle multiple errors generated by the errors.add_to_base method?  Right now, the error is returned as an attribute named 'base', which can be overridden, but I ran into trouble with more than one possible Base error.
</description>
      <pubDate>Thu, 30 Nov 2006 20:04:04 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:32a95ec5-4c3a-44e7-b4e2-23b6a35c2e1e</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-228</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Matt Allen</title>
      <description>Inanc;

You method doesn't work if you hand in an array of modules. 

Try this one. Change line 10 to read

&lt;pre&gt;
object_names.each { |name| objects &amp;lt;&amp;lt; instance_variable_get("@#{name}") if !instance_variable_get("@#{name}").errors.nil? }
&lt;/pre&gt;</description>
      <pubDate>Thu, 30 Nov 2006 16:55:51 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:4019f453-74c4-482c-a0ff-f09ce747872b</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-227</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Inanc Gumus</title>
      <description>Thanks for the plug-in. Notice that if there is no error an exception occurs. This patch beats the nut:&lt;br /&gt;&lt;br /&gt;

        &lt;b&gt;
        # put this at the top of the error_messages_for method
        &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;

        &lt;pre&gt;&lt;code&gt;
        object = instance_variable_get("@#{object_names}")
        return "" if !object || object.errors.empty?
        &lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Tue, 31 Oct 2006 19:58:06 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:b148f6f5-7e38-4aa9-a578-9e712c979e1f</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-211</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by kyle</title>
      <description>Beautiful. Great job.</description>
      <pubDate>Mon, 30 Oct 2006 15:26:32 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d4f7530d-d7b2-4b8b-9f99-49034bf36fcb</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-209</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Petr</title>
      <description>This must be included in future version of Rails. Very good.</description>
      <pubDate>Mon, 09 Oct 2006 02:27:50 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:00c66273-8583-4dfc-8d73-21e02a8215c7</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-183</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Petr</title>
      <description>This must be included in future version of Rails. Very good.</description>
      <pubDate>Mon, 09 Oct 2006 02:27:48 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:87d68381-9dad-4083-9843-6fbf9534cdc0</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-182</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by vr_mex</title>
      <description>it would be very usefull if you could add validating and error message handling when using observe_field</description>
      <pubDate>Tue, 29 Aug 2006 10:03:06 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:f1799434-dcb2-414e-9285-306d1ec80a4c</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-63</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Sebastien</title>
      <description>Thanks for the amazingly helpful plugin! Do you have some more detailed instructions for a newbie such as myself? 

I've changed the Header &amp; subheader but the priority implementation eludes me. 

Thanks for any and all help :)</description>
      <pubDate>Tue, 08 Aug 2006 20:54:50 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:c84e1b7a-ada3-4a49-b60f-a9e3f407b588</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-44</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Chris</title>
      <description>First of all, I really like your plugin. I'm big on the error messages and your plugin makes them very easy to customize. Just one small thing:

* If you use errors.add_to_base, it'll by default display an attr_name of 'Base' at the beginning of your error message. Sure, you could override the error message completely in error_messages_for... but I made a small tweak to the way errors are displayed to get it to play a little nicer:
#{value[0].humanize unless value[0] == 'base'}

</description>
      <pubDate>Mon, 24 Jul 2006 21:12:35 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:a61ba33f-21fd-4aa1-b644-b2f52b84d89b</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-37</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by kris</title>
      <description>Used this, good stuff. Might be nice to put in the readme that you need to copy the folder to vendor/plugins for those who have never done so before :)</description>
      <pubDate>Wed, 19 Jul 2006 04:09:17 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:126dd7f8-d255-44b2-96a3-4064159d9b25</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-31</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by Herman</title>
      <description>Thanks a lot ! Works absolutely great for me.</description>
      <pubDate>Mon, 03 Jul 2006 06:05:37 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:fe446f8e-84af-4b5f-98f3-82aea24ed3e0</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-16</link>
    </item>
    <item>
      <title>"Enhancing Rails Errors" by iqram</title>
      <description>Very useful function.  Saved me a lot of thought and time!  Appreciate it.</description>
      <pubDate>Sun, 25 Jun 2006 12:29:31 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:dcfc848e-a563-4fc1-895b-8584576c699c</guid>
      <link>http://i.nfectio.us/articles/2006/01/26/enhancing_rails_errors#comment-8</link>
    </item>
  </channel>
</rss>
