在Rails 3.2.11中的label_tag中渲染html(html_safe,raw not working)

时间:2013-04-19 06:07:26

标签: ruby-on-rails label

使用:Rails 3.2.11& Ruby 1.8.7

我在尝试使label_tag输出html时遇到了一些严重问题。基本上归结为:

<%= label_tag "This will <strong>not</strong> work!" %>

我试过了:

<%= raw label_tag "This will <strong>not</strong> work!" %>
<%= label_tag raw "This will <strong>not</strong> work!" %>
<%= label_tag "This will <strong>not</strong> work!".html_safe %>
<%= (label_tag "This will <strong>not</strong> work!").html_safe %>

我已经安装了gem'trail_xss'。

没有任何作用!

尽管我可以通过转义html找到许多与raw和html_safe有问题的人无法正常工作相关的问题,但与label_tag无关。我不能在这个问题上使用f.label。

这曾经用于相同的应用程序,但经过一些更新(Rails 3.0.3 - > 3.2.11是主要的)后,它停止工作。我没有注意到这发生的时间,所以我不确定是什么原因引起了这个问题。

你可以复制吗?你有解决方案吗?

1 个答案:

答案 0 :(得分:8)

问题是label_tag的第一个参数应该是标签名称。如果您希望在标记内显示自定义内容,那么它必须是第二个参数。

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-label_tag

试试这个:

<%= label_tag "my label name", raw("This will <strong>not</strong> work!") %>