<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Errors code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 01:31:51 GMT</pubDate>
    <description>DZone Snippets: Errors code</description>
    <item>
      <title>Parsing Errors on Command Line</title>
      <link>http://snippets.dzone.com/posts/show/4998</link>
      <description>Get PHP parsing errors on command line. Useful for those extreme cases where you can't get them to print to the browser.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;find . -name \*.php \! -exec php -l {} \;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Jan 2008 22:35:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4998</guid>
      <author>seanmurphy (Sean Murphy)</author>
    </item>
    <item>
      <title>Testing for Exception message in Rails</title>
      <link>http://snippets.dzone.com/posts/show/4320</link>
      <description>&lt;code&gt;&lt;br /&gt;e = assert_raise(RuntimeError) { my_code_that_raises }&lt;br /&gt;assert_match(/Error message here/i, e.message)..&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 18 Jul 2007 14:39:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4320</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Putting the errors in the right place</title>
      <link>http://snippets.dzone.com/posts/show/3403</link>
      <description>The idea is to display the error near the field instead of in global area at the top of the page.&lt;br /&gt;Simple&#8230; first go into your view and delete the&lt;br /&gt;&lt;code&gt;&lt;br /&gt;error_messages_for &#8216;object&#8217;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then stick this in your application helper.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# application_helper.rb&lt;br /&gt;def error_for(object, method = nil, options={})&lt;br /&gt;  if method&lt;br /&gt;    err = instance_variable_get("@#{object}").errors.on(method).to_sentence     rescue instance_variable_get("@#{object}").errors.on(method)&lt;br /&gt;  else&lt;br /&gt;     err = @errors["#{object}"] rescue nil&lt;br /&gt;  end&lt;br /&gt;  options.merge!(:class=&gt;&#8217;fieldWithErrors&#8217;,&lt;br /&gt;          :id=&gt;"#{[object,method].compact.join(&#8217;_')}-error",&lt;br /&gt;:style=&gt;(err ? #{options[:style]}":"#{options[:style]};display: none;")&lt;br /&gt;   )&lt;br /&gt; content_tag("p",err || "", options )     &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then in your form view, add an &#8216;error_for&#8217; call wherever you need one&#8230;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# _form.rhtml&lt;br /&gt;  &lt;p&gt;&lt;label for="code_project_name"&gt;Name&lt;/label&gt;&lt;br /&gt;  &lt;%= text_field &#8216;code_project&#8217;, &#8216;name&#8217;  %&gt;&lt;br /&gt;  &lt;%= error_for &#8216;code_project&#8217;, &#8216;name&#8217; %&gt;&lt;/p&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If the model fails a validation test, then it will show the message right next to the field that caused the validation problem.&lt;br /&gt;Also note that if you define an instance variable called @errors containing a hash of field_names and messages, they will also be used. This is handy for those form fields that don&#8217;t correspond to a model attribute.</description>
      <pubDate>Thu, 01 Feb 2007 14:06:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3403</guid>
      <author>Mickael (Mickael)</author>
    </item>
    <item>
      <title>Helper to display Rails flashes</title>
      <link>http://snippets.dzone.com/posts/show/2348</link>
      <description>If you use the convention of using :notice, :warning and :error with your flashes, then this simple helper will allow you to display your flashes easily, and it supports the storage of ActiveRecord::Errors in flash[:error] as well.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def display_standard_flashes(message = 'There were some problems with your submission:')&lt;br /&gt;    if flash[:notice]&lt;br /&gt;      flash_to_display, level = flash[:notice], 'notice'&lt;br /&gt;    elsif flash[:warning]&lt;br /&gt;      flash_to_display, level = flash[:warning], 'warning'&lt;br /&gt;    elsif flash[:error]&lt;br /&gt;      level = 'error'&lt;br /&gt;      if flash[:error].instance_of? ActiveRecord::Errors&lt;br /&gt;        flash_to_display = message&lt;br /&gt;        flash_to_display &lt;&lt; activerecord_error_list(flash[:error])&lt;br /&gt;      else&lt;br /&gt;        flash_to_display = flash[:error]&lt;br /&gt;      end&lt;br /&gt;    else&lt;br /&gt;      return&lt;br /&gt;    end&lt;br /&gt;    content_tag 'div', flash_to_display, :class =&gt; "flash #{level}"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def activerecord_error_list(errors)&lt;br /&gt;    error_list = '&lt;ul class="error_list"&gt;'&lt;br /&gt;    error_list &lt;&lt; errors.collect do |e, m|&lt;br /&gt;      "&lt;li&gt;#{e.humanize unless e == "base"} #{m}&lt;/li&gt;"&lt;br /&gt;    end.to_s &lt;&lt; '&lt;/ul&gt;'&lt;br /&gt;    error_list&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 01 Aug 2006 15:54:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2348</guid>
      <author>lukeredpath (Luke Redpath)</author>
    </item>
    <item>
      <title>ActiveRecord::Errors#to_xml</title>
      <link>http://snippets.dzone.com/posts/show/1960</link>
      <description>Here&#8217;s a method that will allow you to call to_xml on an ActiveRecord::Errors object. We&#8217;re using this to pass errors between web apps via a web service api.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ActiveRecord::Errors&lt;br /&gt;  def to_xml(options = {})&lt;br /&gt;    options[:indent] ||= 2&lt;br /&gt;    options.reverse_merge!({ :builder =&gt;&lt;br /&gt;      Builder::XmlMarkup.new(:indent =&gt;&lt;br /&gt;        options[:indent]), :root =&gt; "errors" })&lt;br /&gt;    options[:builder].instruct! unless options.delete(:skip_instruct)&lt;br /&gt;&lt;br /&gt;    options[:builder].__send__(options[:root].to_s.dasherize) do |xml|&lt;br /&gt;      @errors.each do |key, value|&lt;br /&gt;        xml.__send__(key.to_s.dasherize) do |xm|&lt;br /&gt;          for message in value&lt;br /&gt;            xm.message(message)&lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 26 Apr 2006 00:09:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1960</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
    <item>
      <title>Use background color to show error fields instead of wrapping them in a div</title>
      <link>http://snippets.dzone.com/posts/show/1912</link>
      <description>Using the default rails field_error_proc may lead to some layout headaches--your form looks perfect until, uh-oh, someone entered an invalid email address and Rails adds a fieldWithError-styled div that wraps around the problem field.&lt;br /&gt;&lt;br /&gt;While this works in many cases, some pixel-perfect layouts may not be able to tolerate the 2-pixel padding around the text_field caused by the red border.  An alternative is to change the background color of the offending field.&lt;br /&gt;&lt;br /&gt;Include the following code in environment.rb (or use a "require" like I do):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|&lt;br /&gt;  error_style = "background-color: #ffff80"&lt;br /&gt;  if html_tag =~ /&lt;(input|textarea|select)[^&gt;]+style=/&lt;br /&gt;    style_attribute = html_tag =~ /style=['"]/&lt;br /&gt;    html_tag.insert(style_attribute + 7, "#{error_style}; ")&lt;br /&gt;  elsif html_tag =~ /&lt;(input|textarea|select)/&lt;br /&gt;    first_whitespace = html_tag =~ /\s/&lt;br /&gt;    html_tag[first_whitespace] = " style='#{error_style}' "&lt;br /&gt;  end&lt;br /&gt;  html_tag&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;See my blog.inquirylabs.com for more Rails stuff.</description>
      <pubDate>Thu, 13 Apr 2006 22:21:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1912</guid>
      <author>canadaduane (Duane Johnson)</author>
    </item>
  </channel>
</rss>
