ruby-on-railsrubyactiveadminarbre

How to use Rails' simple_format with special characters?


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?


Solution

  • 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