How to use Rails' simple_format with special characters?

532 Views Asked by At

On Rails 3.2.13, simple_format does not return what I expect it to do, on an admittedly convoluted case:

> simple_format("a <= 2, b < 4")
"<p>a &lt; 4</p>"

Since this case does not seem to work properly (I'm losing half my string!), is there a way to pre-escape special characters so that it works everywhere?

1

There are 1 best solutions below

1
On BEST ANSWER

You could html_escape the string yourself:

ERB::Util.h("a <= 2, b < 4")
#=> "a &lt;= 2, b &lt; 4"

simple_format(ERB::Util.h("a <= 2, b < 4"))
#=> "<p>a &lt;= 2, b &lt; 4</p>"

From within views, you can omit ERB::Util. and just call h