Skip to content Skip to sidebar Skip to footer

How To Un-escape Html Strings

So I have a database table that stores pre-escaped text strings (don't ask me why ... I don't know) and I need to put it on the page as a RAW string. All of the rails output funct

Solution 1:

You can use html_safe for the same.

html_safe actually "sets the string" as HTML Safe (it's a little more complicated than that, but it's basically it). This way, you can return HTML Safe strings from helpers or models at will.

h can only be used from within a controller or view, since it's from a helper. It will force the output to be escaped. It's not really deprecated, but you most likely won't use it anymore: the only usage is to "revert" an html_safe declaration, pretty unusual.

Prepending your expression with raw is actually equivalent to calling html_safe on it, but, just like h, is declared on a helper, so it can only be used on controllers and views.

Here's a nice explanation on how the SafeBuffers (the class that does the html_safe magic) work: http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/

Solution 2:

I think you want the raw method:

<%= raw @string %>

Post a Comment for "How To Un-escape Html Strings"