views:

33

answers:

3

I am trying to show a rails view file in a textarea. the view file contains a bunch of HTML, which I want to escape so that it does not interfere with on page html. here is an example:

In this view we are going to display the contents of a partial
<textarea>
<%= html_escape render('partial') %>
</textarea>

and in partial.html.erb I would have

Hello this is partial.html.erb and this is a 
<textarea>textarea</textarea>  blah blah blah.

The problem is: the textarea in partial.html is breaking the textarea in the first view because it is not being html_escaped. How do I property escape and display the contents of the partial inside the textarea?

+1  A: 

Try render_to_string in your controller and then using html_escape on the resulting string.

Jordan
nope, still does not work. the html_escape fails to escape whats returned by render_to_string
Ryan
A: 

Did you try using

<%= CGI.escapeHTML render('partial') %>
Max Schulze
yes. does not work. fwiw h is the same thing as html_escape
Ryan
Just found this: <%= CGI.escapeHTML(render('partial')) %>, does that might solve it? you could also try use CGI.escapeHTML with render_to_string.
Max Schulze
CGI.escapeHTML does work correctly.
Ryan
A: 

Ok - I figured it out. You basically have to call render twice. The first time is to render the file and the second time is to escape that rendered file. This is ugly!

<%= render :text => render("partial") %>

I'd be interested to see if anyone else has any other ways to escape partial.html.

Ryan