# Ruby variable that embeds html tags

**URL:** <https://www.ruby-forum.com/t/ruby-variable-that-embeds-html-tags/209546>\
**Category:** Rails\
**Created:** [July 16, 2011, 12:17pm UTC](https://www.ruby-forum.com/t/ruby-variable-that-embeds-html-tags/209546 "2011-07-16T12:17:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 16, 2011, 12:17pm UTC](https://www.ruby-forum.com/t/ruby-variable-that-embeds-html-tags/209546/1 "2011-07-16T12:17:32Z")

</div>

This seems very simple, but I can’t quite get it. Probably because I’m  
just starting out with RoR.

My view has a slew of labels and text fields; many are “required”:

\<%= f.text\_field :screen\_name %\> Required  
field

(The “required\_field” class turns the text red and smaller.) I’d like  
to not have everything between and sitting at the end  
of every required line. I’d like instead to use a variable:

\<% req = “Required field” %\>

And then have  
\<%= f.text\_field :screen\_name %\> \<%= req %\>

Shorter and DRYer. But when I do that, I get my HTML printed to the  
screen, not interpreted.

Help appreciated. Thanks!  
Jacob

---

<div class="post-metadata">

**Author:** ![jacob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jacob](https://www.ruby-forum.com/u/jacob)\
**Post date:** [July 16, 2011, 1:09pm UTC](https://www.ruby-forum.com/t/ruby-variable-that-embeds-html-tags/209546/2 "2011-07-16T13:09:29Z")

</div>

On 15 July 2011 22:13, Jacob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> of every required line. I’d like instead to use a variable:
> 
> \<% req = “Required field” %\>
> 
> And then have  
> \<%= f.text\_field :screen\_name %\> \<%= req %\>

By default Rails will assume that req may contain malicious text (such  
as some evil js for example) and will escape it so that the raw html  
appears on the page. Since you know that req is safe to output  
directly you can either use \<%= req.html\_safe %\> or \<%= req = “\<span  
… \>”.html\_safe %\>

On a separate point I would use a view helper method rather than  
defining req inline however.

Colin

---

<div class="post-metadata">

**Author:** ![jacob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jacob](https://www.ruby-forum.com/u/jacob)\
**Post date:** [July 18, 2011, 11:45pm UTC](https://www.ruby-forum.com/t/ruby-variable-that-embeds-html-tags/209546/3 "2011-07-18T23:45:25Z")

</div>

Colin L. wrote in post #1011094:

> On 15 July 2011 22:13, Jacob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > of every required line. I’d like instead to use a variable:
> > 
> > \<% req = “Required field” %\>
> > 
> > And then have  
> > \<%= f.text\_field :screen\_name %\> \<%= req %\>
> 
> By default Rails will assume that req may contain malicious text (such  
> as some evil js for example) and will escape it so that the raw html  
> appears on the page. Since you know that req is safe to output  
> directly you can either use \<%= req.html\_safe %\> or \<%= req = “\<span  
> … \>”.html\_safe %\>
> 
> On a separate point I would use a view helper method rather than  
> defining req inline however.

From what I gather from the following it might be slightly faster to use  
\<%= raw req %\> rather than using html\_safe directly when inside a view  
template:

If a plain String is passed into a \<%= %\>, Rails always escapes it

If a SafeBuffer is passed into a \<%= %\>, Rails does not escape it. To  
get a SafeBuffer from a String, call html\_safe on it. The XSS system has  
a very small performance impact on this case, limited to a guard calling  
the html\_safe? method

If you use the raw helper in a \<%= %\>, Rails detects it at compile-time  
of the template, resulting in zero performance impact from the XSS  
system on that concatenation

Rails does not escape any part of a template that is not in an ERB tag.  
Because Rails handles this at template compile-time, this results in  
zero performance impact from the XSS system on these concatenations

---

<div class="post-metadata">

**Author:** ![jacob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jacob](https://www.ruby-forum.com/u/jacob)\
**Post date:** [July 19, 2011, 9:26am UTC](https://www.ruby-forum.com/t/ruby-variable-that-embeds-html-tags/209546/4 "2011-07-19T09:26:47Z")

</div>

On 18 July 2011 22:45, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > as some evil js for example) and will escape it so that the raw html
> 
> Rails does not escape any part of a template that is not in an ERB tag.  
> Because Rails handles this at template compile-time, this results in  
> zero performance impact from the XSS system on these concatenations

That is useful to know, thanks Robert.

Colin
