简体   繁体   中英

Rails 4: Link_to tag not creating appropriate html

I'm using Bootstrap's tooltip to show glyphicons.

Hardcoded, this works as expected

    <%= link_to "<h2>test</h2>",
                        '#',
                        class: "tag-tooltip",
                        :data => {:toggle=>"tooltip"},
                        'data-original-title' => 
    "<i class=\"icon-off icon-white\"></i> = SomeText
    <i class=\"icon-info-sign icon-white\"></i> = SomeText
    <i class=\"icon-ban-circle icon-white\"></i> = SomeText
    <i class=\"icon-warning-sign icon-white\"></i> = SomeText
    <i class=\"icon-remove icon-white\"></i> = SomeText
    <i class=\"icon-refresh icon-white\"></i> = SomeText
    <i class=\"icon-download-alt icon-white\"></i> = SomeText
    <i class=\"icon-list-alt icon-white\"></i> = SomeText
    <i class=\"icon-ok icon-white\"></i> = SomeText",
                        'data-placement' => 'top' %>

Now I'll use a helper method to achieve the same thing.

<%= link_to 'First', '#', class: 'tag-tooltip', :data => {:toggle=>"tooltip"}, 'data-original-title' => get_icon_tooltip(get_icon_hash).html_safe, 'data-placement' => 'top' %>

Now, despite the html being the same for the icons

<i class="icon-off icon-white"></i> = SomeText<i class="icon-info-sign icon-white"></i> = SomeText<i class="icon-ban-circle icon-white"></i> = SomeText<i class="icon-warning-sign icon-white"></i> = SomeText<i class="icon-remove icon-white"></i> = SomeText<i class="icon-refresh icon-white"></i> = SomeText<i class="icon-download-alt icon-white"></i> = SomeText<i class="icon-list-alt icon-white"></i> = SomeText<i class="icon-ok icon-white"></i> = SomeText 

the end result is HTML with unescaped '<' and '>' characters.

<a class="tag-tooltip" data-original-title="<i class="icon-off icon-white"></i> = SomeText<i class="icon-info-sign icon-white"></i> = SomeText<i class="icon-ban-circle icon-white"></i> = SomeText<i class="icon-warning-sign icon-white"></i> = SomeText<i class="icon-remove icon-white"></i> = SomeText<i class="icon-refresh icon-white"></i> = SomeText<i class="icon-download-alt icon-white"></i> = SomeText<i class="icon-list-alt icon-white"></i> = SomeText<i class="icon-ok icon-white"></i> = SomeText" data-placement="right" data-toggle="tooltip" href="#">First</a>

Thoughts?

Why are you calling html_safe on your helper ? This will cause the unescaped characters.

Can you replace :

get_icon_tooltip(get_icon_hash).html_safe

by

get_icon_tooltip(get_icon_hash)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM