在 Rails 3 应用程序中,我有一个域类,其中一个属性存储纯 HTML 内容(它是一个博客应用程序,域类是 Post)。
在 ERB 模板中,我需要显示格式化后的属性内容,并放置 HTML 标签。但是,Rails 正在转义所有 HTML 标签!如何禁用此类属性的此行为?
示例:
somePost = Post.new
somePost.content = "<strong> Hi, i'm here! </strong>"
在 erb 模板中:
<%= somePost.content %>
生成的 HTML 被转义:
<strong> Hi, i'm here! </strong>
最佳答案
尝试使用raw(somePost.content)
。或者,somePost.content.html_safe
。
关于ruby-on-rails-3 - 在 erb 模板中禁用 HTML 转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4699497/