Skip to content

Instantly share code, notes, and snippets.

@nistude
Forked from bernd/gist:2d5563f51586156dcc81
Created September 5, 2012 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nistude/65e767d9d5b3283a0271 to your computer and use it in GitHub Desktop.
Save nistude/65e767d9d5b3283a0271 to your computer and use it in GitHub Desktop.
Monkey patch to handle regression in ERB::Util#html_escape in rails 3.0.17 / ruby 1.9
class ERB
module Util
# see https://github.com/rails/rails/issues/7430 for details
#
# A utility method for escaping HTML tag characters.
# This method is also aliased as <tt>h</tt>.
#
# In your ERB templates, use this method to escape any unsafe content. For example:
# <%=h @person.name %>
#
# ==== Example:
# puts html_escape("is a > 0 & a < 10?")
# # => is a &gt; 0 &amp; a &lt; 10?
def html_escape(s)
s = s.to_s
if s.html_safe?
s
else
s.gsub(/[&"'><]/, HTML_ESCAPE).html_safe
end
end
alias h html_escape
singleton_class.send(:remove_method, :html_escape)
module_function :html_escape, :h
end
end
@grosser
Copy link

grosser commented Feb 5, 2013

Thanks you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment