The standard thing to do is put classes in there instead of strings,
so each error would be an instance of class Error, which would respond
to to_xml with <error>foo</error> or whatever.  See, for example, Pat
Maddox's Blog: 
http://evang.eli.st/blog/2007/2/22/my-rails-gotcha-custom-to_xml-in-a-hash-or-array

If you are worried about reversibility, you can change active_support
\core_ext\array\conversions so that the to_xml method looks like this
at the top:

          options[:root]     ||= all? { |e| e.is_a?(first.class) &&
first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize :
"records"
          map!{|e| (e.class.name == "String") ? {options[:root].to_sym
=> e} : e}

          raise "Not all elements respond to to_xml" unless all? { |e|
e.respond_to? :to_xml }

          options[:children] ||= options[:root].singularize
          options[:indent]   ||= 2
          options[:builder]  ||= Builder::XmlMarkup.new(:indent =>
options[:indent])

The XML you get back isn't pretty, but it doesn't crash:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<hash>
  <errors>
    <error type=\"array\">
      <error>
        <error>foo</error>
      </error>
      <error>
        <error>bar</error>
      </error>
    </error>
  </errors>
</hash>

Cleaning it up more than this might be a bit tricky.  If this is
something a lot of people would like, I'd look at it some more, but I
think it's pretty unusual, and it's usually easier to use objects.

-Michael

On Oct 6, 7:54 pm, Sam <[EMAIL PROTECTED]> wrote:
> I'm wanting something like...
>
> <errors>
>   <error>foo</error>
>   <error>bar</error>
> </errors>
>
> I find it really interesting that a Rails controller will accept xml
> like...
>
> <process-codes>
>   <code>one</code>
>   <code>two</code>
> </process-codes>
>
> .. and transform it into a hash..
>
> { :process_codes => { :code => [ "one", "two" ] } }
>
> ... but will not go back the other way when I want to convey errors...
>
> { :errors => { :error => [ "foo", "bar" ] } }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to