views:

60

answers:

1

What's the best way to handle having two templates (or should it be one, DRY?) for xml builder templates?

I'm building a web api with Rails and wanted to see an example of how to have a view that does regular output vs one that does error output. I've been using @obj.to_xml for a while, but my requirements have changed and require me building my own error templates.

do you typically have both views in one with a condition above for errors such as

app/views/myresource/create.xml.builder

unless @myobj.errors.empty? // xml for errors here? end

Can someone show me an xml.builder view example that has a case handled for when there is an error with an ActiveRecord object and or when it's successful?

// regular xml view

+1  A: 

you can try something like this:

 def create
   ....
   return render :template=>'error' unless @myobj.errors.empty?
   ....
   other code
 end

in this case rails will render error template (it can be shared across all project) if you have an error, and render create builder if there is no error.

potapuff