简体   繁体   中英

Encode/escape HTML string in Ruby on Rails

I have a string that came from a field in my model.

# <br />
"This is my title with break &lt;br /&gt; in the middle"

I'm rendering it on my view:

<p>
  <%= raw @model.title %>
</p>

Which outputs something like this:

<p>
  This is my title with break <br /> in the middle
</p>

However, the <br /> shows up as string and not as html. Any ideas?

Thanks!

Yes, as Dave Newton said, you should unescape then use html_safe. If your html in your model is not complex, you could use CGI.unescapeHTML to unescape. But if it is, I think you should use gem HTMLEntities at http://htmlentities.rubyforge.org/

<p>
  <%= (CGI.unescapeHTML @model.title).html_safe %>
</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM