如何使erb标签中的html代码无法转义 [英] How to make html code in erb tag not escaped

查看:88
本文介绍了如何使erb标签中的html代码无法转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails项目的其中一个视图中,我有一些简单的erb代码。

I have some simple erb code in one of my views in a rails project.

<%= comment.body%>

我希望comment.body中的html标签可以保留,因为它们具有格式信息。我已验证文本是否正确保存在数据库中,例如

I'd like the html tags in the comment.body to be preserved as they have formatting information. I've verified that the text is saved in the database properly like

< b> hello< / b>

但是在页面上显示的是< b> hello< / b> 而不是您好,正如我所期望的。

However it turns out on the page to be <b>hello</b> not hello as I expect.

怎么可能?我没有使用<%= h 来转义html代码。

How could this be? I'm not using <%= h to escape the html code.

如何使其不逃逸?我正在使用Rails3。这有关系吗?

How do I make it not escaping? I'm using rails 3. Does this matter?

推荐答案

Rails 3现在会自动转义您的输出。

Rails 3 now automatically escapes your output.

取消转义文本并使用实际标签,请使用 raw(...)

To unescape the text and use the actual tags, use raw(...):

<%= raw(comment.body) %>

但是,请注意这一点,因为它将允许任何标签,包括脚本(可能是恶意的)。较安全的选择可能是让用户使用markdown格式的文本或类似内容,而不是允许原始HTML标签。

However, be careful with this, as it will allow any tags, including scripts (potentially malicious). A safer option might be to have users use markdown-formatted text or something similar, rather than allowing raw HTML tags.

这篇关于如何使erb标签中的html代码无法转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆