Friday, 29 April 2011

html_escape in RoR

You can escape html entities from your code using Rails html_escape() method.

<%= html_escape ("<p> will remain as it is.")  %>
OR
<%=h  "<p>will remain as it is." %>

h() is a short tag for html_escape.
BUT IN RAILS 3 html_escape is used by default. 

This can be  annoying when you need to add some html stuff on your DOM as in such cases it'll render as simple text.

How to overide default html_escape behaviour of rails 3?
By using rails  html_safe method
<%= "<p> my string".html_safe %>

Reverse of html_escape?
CGI class has some great inbuilt function. One of them does allow you to do that.

CGI::unescapeHTML
require 'cgi'
CGI::unescapeHTML("&lt;it's me&gt;") 
=> <it's me >

No comments:

Post a Comment