Forum: Ruby on Rails View into a String?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Duane M. (Guest)
on 2006-06-09 19:19
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.
Ben K. (Guest)
on 2006-06-09 19:24
Jón B. (Guest)
on 2006-06-09 19:32
(Received via mailing list)
Without verifying or looking up. I think this should work.

xmlstring = render(:action => "xmlview").to_s


On 6/9/06, Duane M. <removed_email_address@domain.invalid> wrote:
> to be creating a race condition.
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Jón B. (Guest)
on 2006-06-09 19:32
(Received via mailing list)
Oh... A better and a more correct idea came forward.

On 6/9/06, Jon Gretar B. <removed_email_address@domain.invalid> wrote:
> > output into a string" instead of writing it back to the browser?  After
> > _______________________________________________
>
--
Duane M. (Guest)
on 2006-06-09 21:08
Ben Kittrell wrote:
> Try render_to_string
> http://api.rubyonrails.com/classes/ActionControlle...

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.
This topic is locked and can not be replied to.