# Unusual rendering behavior with render\_to\_string()

**URL:** <https://www.ruby-forum.com/t/unusual-rendering-behavior-with-render-to-string/75460>\
**Category:** Rails\
**Created:** [November 10, 2006, 12:13am UTC](https://www.ruby-forum.com/t/unusual-rendering-behavior-with-render-to-string/75460 "2006-11-10T00:13:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 10, 2006, 12:13am UTC](https://www.ruby-forum.com/t/unusual-rendering-behavior-with-render-to-string/75460/1 "2006-11-10T00:13:32Z")

</div>

I have an app. where I am generating PDF files. I use a .rxml template  
to generate content that I then write to a file using  
render\_to\_string().

That works fine, however, if I make a call to render(:text =\> ‘blah’)  
afterwards, I see an error that makes it look like the render is  
identifying the response as XML, even though render\_to\_string is not  
supposed to affect the action response to the browser.

Here’s my code:

```
File.open(pdfgen.xfdf_file, "w") do |f|
  f.write(render_to_string(:layout => false))
end

begin
  pdfgen.generate_pdf
rescue
  return render(:text => 'There was an error generating your quote', 

```

:layout =\> false)  
end

If the rescue clause runs, I get this:

"XML Parsing Error: syntax error  
Location: [http://localhost:3001/artisan\_qualifiers/2805/print\_quote](http://localhost:3001/artisan_qualifiers/2805/print_quote)  
Line Number 1, Column 1:

There was an error generating your quote"

However, if I comment the call to render\_to\_string out, the text will  
generate normally. Almost as if the call to render\_to\_string somehow  
caused some internal flag to be set that the response would be XML, even  
though it won’t.

So, two questions:

1. Does render\_to\_string() modify the response even though the API docs.  
say that it doesn’t?

2. Is there an alternative/better way to render the output of my RXML  
file to another file?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 10, 2006, 10:32am UTC](https://www.ruby-forum.com/t/unusual-rendering-behavior-with-render-to-string/75460/2 "2006-11-10T10:32:21Z")

</div>

I forced the Content-Type to “text/plain” like so, and that work’s well.

```
  headers["Content-Type"] = 'text/plain'

```

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 28, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/unusual-rendering-behavior-with-render-to-string/75460/3 "2007-02-28T15:48:44Z")

</div>

Wes,

How did you force the content headers? I’m having an unrelated  
problem in that I have a Rails view that’s being returned as text/  
plain instead of text/html. So I was wondering if I could force it to  
text/html. Do you know how to do that in a Rails app?

Thanks,

Zeff

On Nov 10 2006, 4:32 am, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [February 28, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/unusual-rendering-behavior-with-render-to-string/75460/4 "2007-02-28T21:08:35Z")

</div>

I’ve removed that code since - so I don’t have an example from my app.  
anymore, but if memory serves, you should just be able to put a line  
like that into your controller action.

Wes
