6

我有一个 Rails 3 助手,它需要生成属性值中可能包含 HTML 实体的元素。例如:

content_tag(:input, nil, :type => 'button', :value => 'Redo ↻')

我最终得到了这个:<input type="button" value="Redo &#x21bb;" />
但我想要的是:<input type="button" value="Redo ↻" />

如何告诉 Rails 不要在我的内容标签属性中转义 HTML 实体?

4

1 回答 1

14

默认情况下,rails 在打印内容之前会对其进行转义。您必须明确声明内容可以安全输出:

您可以使用原始助手或字符串方法 html_safe 来完成:

content_tag(:input, nil, :type => 'button', :value => 'Redo &#x21bb;'.html_safe)

或者

content_tag(:input, nil, :type => 'button', :value => raw('Redo &#x21bb;'))
于 2013-07-25T23:09:08.583 回答