在视图中,字符串在默认情况下是转义的。
mystring = "A&B: <b>some string here</b>"
<%=mystring%>mystring呈现为:
A&B: <b>some string here</b>但是,我需要呈现<b></b>标记并对“与”进行转义。
A&B: <b>some string here</b>html_safe不转义and和<b>标记。有没有一种方法可以转义特殊的字符,比如&符号,而不是html标签?
发布于 2012-04-14 12:41:32
您可以使用Ruby的CGI::unescape方法对特定元素进行取消转义。在您的示例中,您需要使用以下内容:
mystring = CGI::escape_html("A&B: <b>some string here</b>")
# You can replace ["B"] with an array of tags to be escaped, i.e. ["B", "A", "IMG"]
mystring = CGI::unescapeElement(mystring, ["B"])
<%= mystring.html_safe %>有关更多转义方法,请参阅http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html。
发布于 2012-04-14 12:22:41
您可以将字符串拆分为多个部分,然后对所需的部分进行转义
https://stackoverflow.com/questions/10150932
复制相似问题