Rails 4:Link_to标记没有创建适当的html

时间:2014-03-26 16:14:54

标签: ruby-on-rails ruby-on-rails-4

我正在使用Bootstrap的工具提示来显示glyphicons。

硬编码,这可以按预期工作

    <%= 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' %>

现在我将使用辅助方法来实现同样的目的。

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

现在,尽管图标的html是相同的

<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 

最终结果是带有未转义的'&lt;'的HTML和'&gt;'字符。

<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>

思想?

1 个答案:

答案 0 :(得分:1)

为什么要在助手上拨打html_safe?这将导致未转义的字符。

你可以替换:

get_icon_tooltip(get_icon_hash).html_safe

通过

get_icon_tooltip(get_icon_hash)