如何HTML编码/转义字符串?有内置吗?

问题描述:

我有一个不可信的字符串,我想在HTML页面中显示为文本。我需要作为HTML实体将字符'< '和'& '。

I have an untrusted string that I want to show as text in an HTML page. I need to escape the chars '<' and '&' as HTML entities. The less fuss the better.

我正在使用UTF8,不需要其他实体用于重音字母。

I'm using UTF8 and don't need other entities for accented letters.

在Ruby或Rails中有内置函数,还是应该自己滚动?

Is there a built-in function in Ruby or Rails, or should I roll my own?

h 帮助方法:

<%=h "<p> will be preserved" %>