本文介绍了在 erb 模板中禁用 HTML 转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Rails 3 应用程序中,我有一个域类,其中一个属性存储纯 HTML 内容(它是一个博客应用程序,域类是 Post).
In a Rails 3 application I have a domain class where one attribute stores pure HTML content (it's a blog app, the domain class is Post).
在 ERB 模板中,我需要显示格式化时的属性内容,并带有 HTML 标签.但是,Rails 正在转义所有 HTML 标签!如何为此类属性禁用此行为?
In the ERB templates, I need to display the content of the attribute as it was formmated, with the HTML tags in place. But, Rails is escaping all HTML tags! How can I disable this behaviour for this class attribute?
示例:
somePost = Post.new
somePost.content = "<strong> Hi, i'm here! </strong>"
在erb模板中:
<%= somePost.content %>
生成的 HTML 被转义:
The HTML generated is escaped:
<strong> Hi, i'm here! </strong>
推荐答案
尝试使用 raw(somePost.content)
.或者,somePost.content.html_safe
.
这篇关于在 erb 模板中禁用 HTML 转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!