0

so i'm trying to send xml back from my controller..

render xml: ['hello world']

correctly gives me:

<?xml version="1.0" encoding="UTF-8"?>
<strings type="array">
  <string>hello world</string>
</strings>

however

render xml: 'hello world'

gives xml headers but the body is just:

hello world

which is not xml format.

bug?

2
  • I don't think it's a bug. Every XML document needs a root element, and I don't string is a valid option for a root. Commented Aug 3, 2012 at 21:08
  • I think you can just do something like render xml: "<response>string content</response>" Commented Aug 3, 2012 at 21:26

1 Answer 1

2

From the API documentation:

When a request comes in, for example for an XML response, three steps happen:

1) the responder searches for a template at people/index.xml;

2) if the template is not available, it will invoke #to_xml on the given resource;

3) if the responder does not respond_to :to_xml, call #to_format on it.

See: http://api.rubyonrails.org/classes/ActionController/Responder.html

In Rails, Arrays respond to to_xml but Strings do not.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that makes sense. What's best practice here then? All xml responses should be wrapped in an arbitrary root as ismaelga suggested? Though I'm still not convinced this is expected behavior.. <string>hello world</string> sounds like very valid xml to me
What if you're doing some simple ajax and you just want to return a string to the DOM. Does convention really state that I should write a new partial file and render the partial?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.