Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, insert span tag in form_for label custom text

My problem is I wanted to insert a <span> tag in form_for label custom text. In normal html code, it would be like this:

<label>Address<span class="required">*</span></label>

but it Rails, how would I insert it in here:

<%= f.label :address, "Address" %>

It's just an indication for required fields.

like image 931
Charlie Avatar asked May 29 '14 16:05

Charlie


2 Answers

As most of the Form helpers, you can pass a do/block instead of the name argument:

<%= f.label :address do %>
  Address<span class="required">*</span>
<% end %>

Works with link_to also:

<%= link_to root_path do %>
  <div>Hey!</div>
<% end %>
like image 106
MrYoshiji Avatar answered Oct 30 '22 08:10

MrYoshiji


You can just do like this

<%= f.label :address, "Address<span class='required'>*</span>".html_safe %>

This produces the following HTML

<label for="address">Address<span class="required">*</span></label>

OR

You can do like this too.

<%= f.label :address do %>
   Address<span class="required">*</span>
<% end %>
like image 7
Pavan Avatar answered Oct 30 '22 07:10

Pavan