Render html in label_tag in Rails 3.2.11 (html_safe, raw not working)

你。 提交于 2019-12-07 02:15:28

问题


Using: Rails 3.2.11 & Ruby 1.8.7

I am having some serious problem trying to make label_tag output html. Basically it boils down to:

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

I have tried:

<%= 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 %>

I have installed the gem 'rails_xss'.

Nothing works!

Even though I can find a lot of related problems with escaping html where people having problems with raw and html_safe not working, nothing is related to label_tag. I cannot use f.label for this issue.

This used to work on the same application but after a few updates (where Rails 3.0.3 -> 3.2.11 was the major one) it stopped working. I didn't notice when this happened so I am not sure what caused the problem.

Can you replicate? Do you have a solution?


回答1:


The problem is that the first argument to label_tag is supposed to be the label name. If you wish to display custom content inside the tag, that must be the second argument.

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

Try this:

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


来源:https://stackoverflow.com/questions/16098290/render-html-in-label-tag-in-rails-3-2-11-html-safe-raw-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!