Rails Avoiding and Displaying the Content of a Rendered View

I am trying to show a rail view file in a text box. The view file contains a bunch of HTML that I want to escape so that it doesn't interfere with the html page. 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 will have

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

The problem is that textarea in partial.html splits the text field in the first view, because it is not html_escaped. How do I display a property and display the contents of a part inside a text box?

+3
source share
5 answers

You tried to use

<%= CGI.escapeHTML render('partial') %>
+5
source

render_to_string , html_escape .

+1

, . . , - . !

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

, - partial.html.

0

, , :  ... sanitize instance.yourtext% > "sanitize" "Agile web design with Ruby on Rails" ., . . . , ...

0

EDITED Rails, text_area HTML, script. , <textarea>

<%= f.text_area :model_attribute %>

Note. If you need to see raw HTML in the text area, just enable the escape option and set it to false to disable HTML escaping.

<%= f.text_area :model_attribute, escape: false %>

Citation: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_area_tag

When using this function, I recommend checking or excluding the script tag from the presented value if the source is not trusted or open to the public.

0
source

Source: https://habr.com/ru/post/1771379/


All Articles