Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

<%= f.label :test %>

Requested output:

<label for="test">test<span>*</span></label>
like image 466
shub Avatar asked Jul 14 '11 08:07

shub


2 Answers

Label accepts a block, so you can do this:

<%= f.label :test do %>
   <span>*</span>
<%end%>
like image 122
T C Avatar answered Oct 23 '22 06:10

T C


You need to tell rails that the string you're giving it does not need to be sanitized. Use String#html_safe.

<%= f.label :test, "test<span>*</span>".html_safe %>
like image 22
edgerunner Avatar answered Oct 23 '22 06:10

edgerunner