开发者

RoR - Rendering nested errors on XML

开发者 https://www.devze.com 2022-12-10 07:26 出处:网络
Good afternoon, I开发者_开发百科\'m trying to render as XML the complete ActiveRecord error list, problem is when you do something like:

Good afternoon,

I开发者_开发百科'm trying to render as XML the complete ActiveRecord error list, problem is when you do something like:

respond_to do |format|
   format.xml { render :xml => @object }
end

It does not render nested attributes if you don't say so, so either: you should create a template or calling explicity to_xml method and using ":include". This last option seems to work fine with nested attributes on model associations. But what if we got errors? This code does not work:

respond_to do |format|
   format.xml { render :xml => @client.to_xml(:include => :errors }
end

I know I could do @client.errors and even hide .to_xml, but now i want to do something like:

   respond_to do |format|
       format.xml { render :xml => @client.to_xml(:include => {
                                        :errors, 
                                        :client_contact => {:include => :errors } } )}
    end

And supposedly I could obtain only in 1 xml, the errors from the client, and the errors from the client.client_contact! Let me know if i'm doing something wrong, or this :include is not supposed to work with errors

Regards


Have a look at the documentation for XML builder in the API docs. You can generate XML based on any number of conditions and output it however you like.

There's also a Railscasts episode showing you how to do a similar thing for RSS feeds.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号