Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 881
Christoffer Avatar asked Apr 19 '13 06:04

Christoffer


1 Answers

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!") %>
like image 104
mchail Avatar answered Oct 25 '22 13:10

mchail