Rails, insert span tag in form_for custom label text

My problem is that I wanted to insert the <span> in the custom text of the form_for label . In normal html code it will look like this:

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

but this is Rails, how to insert it here:

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

This is just an indication for required fields.

+6
source share
2 answers

Like most form helpers, you can pass 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 %> 
+8
source

You can just do it

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

This results in the following HTML

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

OR

You can do it too.

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

Source: https://habr.com/ru/post/970039/


All Articles