How to use an HTML entity in a Rails "content_tag" attribute?
Asked Answered
H

1

6

I've got a Rails 3 helper which needs to generate elements which might have HTML entities in the attribute values. For example:

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

I end up with this: <input type="button" value="Redo &#x21bb;" />
but what I want is: <input type="button" value="Redo ↻" />.

How can I tell Rails not to escape HTML entities in my content tag attributes?

Hylotheism answered 25/7, 2013 at 23:2 Comment(0)
F
14

By default rails escapes the content before printing it. You have to explicitly declare the content is safe to ouput:

You can do it using the raw helper, or the string method html_safe:

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

or

content_tag(:input, nil, :type => 'button', :value => raw('Redo &#x21bb;'))
Flake answered 25/7, 2013 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.