How to use an HTML object in the "content_tag" Rails attribute?

I have a Rails 3 helper that needs to generate elements that can have HTML objects in attribute values. For instance:

content_tag(:input, nil, :type => 'button', :value => 'Redo ↻') 

As a result, I get the following: <input type="button" value="Redo &#x21bb;" /> <input type="button" value="Redo &#x21bb;" />
but I want: <input type="button" value="Redo ↻" /> .

How can I tell Rails not to remove HTML elements in the attributes of the content tag?

+4
source share
1 answer

By default, the rails slip away from the contents before printing. You must explicitly declare that the content is safe to exit:

You can do this using the helper helper or the html_safe string method:

 content_tag(:input, nil, :type => 'button', :value => 'Redo &#x21bb;'.html_safe) 

or

 content_tag(:input, nil, :type => 'button', :value => raw('Redo &#x21bb;')) 
+9
source

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


All Articles