【发布时间】:2012-03-23 05:16:42
【问题描述】:
在 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>
我看过这些帖子并尝试实施他们的修复:
- raw vs. html_safe vs. h to unescape html
- Disable HTML escaping in erb templates
- How to make html code in erb tag not escaped
我还观看了关于 xss 保护的 Ryan Bates Railscasts 剧集:http://railscasts.com/episodes/204-xss-protection-in-rails-3?view=comments
我根据他的示例创建了一个名为 safe 的辅助方法,我确保字符串内容应用了 html_safe 方法:
def safe(content)
"#{content}".html_safe
end
然后我在我的模型上调用它:safe(@object.property)
内容仍然没有按预期显示。
我也尝试过使用 sanitize 方法,但无济于事。
这可能是什么原因造成的?
【问题讨论】:
-
你期待什么输出,究竟是什么?您的前两个代码示例完全符合他们的预期。你真的是说你想让它逃脱吗? (例如
&lt;strong&gt;Some content...) -
只是试图让实际的 html 显示 - 而不是标签。更新了具体问题。
标签: ruby-on-rails ruby-on-rails-3