How can I put an HTML tag inside <label> in Ruby on Rails?

How can I put a tag <span>inside a tag f.label?

<%= f.label :test %>

The requested output:

<label for="test">test<span>*</span></label>

Thank you and welcome.

+5
source share
3 answers

You need to indicate the rails that the line you give it does not need to be disinfected. Use String#html_safe.

<%= f.label :test, "test<span>*</span>".html_safe %>
+7
source

The label accepts the block, so you can do this:

<%= f.label :test do %>
   <span>*</span>
<%end%>
+16
source

This should do it:

<%= f.label :test, 'test<span>*</span>'.html_safe %>
+1
source

All Articles