# View into a String?

**URL:** <https://www.ruby-forum.com/t/view-into-a-string/62930>\
**Category:** Rails\
**Created:** [June 9, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/view-into-a-string/62930 "2006-06-09T17:19:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [June 9, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/view-into-a-string/62930/1 "2006-06-09T17:19:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [June 9, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/view-into-a-string/62930/2 "2006-06-09T17:24:34Z")

</div>

Try render\_to\_string  
[http://api.rubyonrails.com/classes/ActionController/Base.html#M000207](http://api.rubyonrails.com/classes/ActionController/Base.html#M000207)

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [June 9, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/view-into-a-string/62930/3 "2006-06-09T17:32:32Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:

> to be creating a race condition.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [June 9, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/view-into-a-string/62930/4 "2006-06-09T19:08:09Z")

</div>

Ben Kittrell wrote:

> Try render\_to\_string  
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionController/Base.html#M000207)

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.

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [June 9, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/view-into-a-string/62930/5 "2006-06-09T17:32:45Z")

</div>

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

On 6/9/06, Jon Gretar B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

–
