View into a String?

I’ve got a nice XML view of my data that works just fine when I call it
from the browser. Now I’d like a different controller method to take
that XML and write it to the file system. Since the XML view lives in
an RXML file, is there a trick to telling the controller “Get your
output into a string” instead of writing it back to the browser? After
writing it out to the file system I have a different page I want to
write.

Right now I’m actually making a Net::HTTP call to myself, but that seems
to be creating a race condition.

Try render_to_string
http://api.rubyonrails.com/classes/ActionController/Base.html#M000207

Without verifying or looking up. I think this should work.

xmlstring = render(:action => “xmlview”).to_s

On 6/9/06, Duane M. [email protected] wrote:

to be creating a race condition.


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Ben Kittrell wrote:

Try render_to_string
Peak Obsession

Aha! Neat find, thanks. Though I think this might be trickier than it
first appears. You apparently can’t use both “render_to_string” and
“render” in the same request. So, for isntance, I cannot render my xml
to a string, save it to the file system, and the go on to render a
response. Since my view is an RXML file, it appears to set the
returning contentType to text/xml on the render_to_string statement,
which would be a mistake. Because then when I go to render my HTML
results, it starts barfing wherever I have poorly formed XHTML.
(I can prove this because if I render valid xml, it works fine.)

This’ll work for me for now, I’ll just have to come up with some way
(possibly via redirect) to refresh the page when done. But really I
don’t think that render_to_string should set the contenttype header.

Oh… A better and a more correct idea came forward.

On 6/9/06, Jon Gretar B. [email protected] wrote:

output into a string" instead of writing it back to the browser? After