2014-04-05 21 views
0

我试图使用数据属性向链接添加字体图标。以文本形式输出数据属性的导轨

这是我的rails模板

<%= link_to "Profile", current_user, :data => { :icon => '&#xe609;' } %> 

这是得到的HTML

<a data-icon="&amp;#xe61c;" href="/canvases/8">Preview</a> 

对于工作图标输出,这是被输出(注意&现在是一个&)

<a data-icon="&#xe61e;" href="/canvases/8"></a> 

它看起来像这样:

enter image description here

回答

1

数据属性默认为HTML编码。这可以防止XSS。您可以将该字符串标记为安全。

<%= link_to "Profile", current_user, :data => { :icon => '&#xe609;'.html_safe } %> 

这是为硬编码字符串找到的。但请注意,如果您使用用户提供的数据可能会很危险。

+0

我使用用户提供的数据吗?是?如果是这样,是否有其他解决方案? – colmtuite