相关疑难解决方法(0)

在erb模板中禁用HTML转义

在Rails 3应用程序中,我有一个域类,其中一个属性存储纯HTML内容(它是一个博客应用程序,域类是Post).

在ERB模板中,我需要在格式化时显示属性的内容,并使用HTML标记.但是,Rails正在逃避所有HTML标签!如何为此类属性禁用此行为?

例:

somePost = Post.new
somePost.content = "<strong> Hi, i'm here! </strong>"
Run Code Online (Sandbox Code Playgroud)

在erb模板中:

<%= somePost.content %>
Run Code Online (Sandbox Code Playgroud)

生成的HTML被转义:

&lt;strong&gt; Hi, i'm here! &lt;/strong&gt;
Run Code Online (Sandbox Code Playgroud)

escaping erb ruby-on-rails-3

39
推荐指数
3
解决办法
3万
查看次数

Rails 3 - raw和html_safe的问题

在Rails 3.0.10中使用raw和html_safe方法,我仍然无法取消html并将其显示为Content而不是<strong>Content</strong>.

@object.property.html_safe 给我 <strong>Some content</strong>

<%= raw(@object.property) %> 也给了我 <strong>Some content</strong>

我看过这些帖子并试图实施他们的修复:

我还看过关于xss保护的Ryan Bates Railscasts插曲:http://railscasts.com/episodes/204-xss-protection-in-rails-3?view = comment

我创建了一个基于他的示例安全的帮助方法,我确保字符串内容应用了html_safe方法:

def safe(content)
    "#{content}".html_safe
end
Run Code Online (Sandbox Code Playgroud)

然后我在我的模型上调用它: safe(@object.property)

内容仍未按预期显示.

我也尝试过使用sanitize方法,但无济于事.

可能是什么导致了这个?

ruby-on-rails ruby-on-rails-3

3
推荐指数
1
解决办法
4450
查看次数

标签 统计

ruby-on-rails-3 ×2

erb ×1

escaping ×1

ruby-on-rails ×1