2013-10-31 26 views
0

我在application_helper创建一个定义一个标准的link_to这样的:禁用HTML中的一个辅助功能逃逸

module ApplicationHelper 
    def foo_link_to(text, path) 
     out = "<span class=\"span\">" 
     out += link_to text, path 
     out += "</span>" 
     out 
    end 
end 

,并在我的部分,我有

<%= foo_link_to 'text', home_path %> 

但是这是我的输出

&lt;span class=&quot;span&quot;&gt;&lt;a href=&quot;/home&quot;&gt;index&lt;/a&gt;&lt;/span&gt; 

现在,我的问题是:在哪里需要插入html_escape?
ty全部

谢谢各位的支持。 现在我有另一个问题...如果我不会这个输出我必须做什么?

<a href="home.html"><i class="iclass"></i><span class="span"> text </span></a> 

使用raw outout.html_safe输出

a href="/home">/home</a>&lt;span class=&quot;span&quot;&gt;&lt;i class=&quot;iclass&quot;&gt;text&lt;/i&gt;&lt;/span&gt; 

回答

2

使用raw,在最后一行

raw out 

而且你的帮手,可以进一步重构为

def foo_link_to(text, path) 
    content_tag :span do 
    link_to text, path 
    end 
end 

我忘记了,但在后面的例子中似乎你不需要raw

更新:在过去的图标之一,你可以这样

link_to 'home.html' do 
    content_tag :i, class: 'iclass' 
    content_tag :span, class: 'span' do 
    'Text' 
    end 
end 
+0

@gotva,感谢您的编辑。我认为'class =“span”'在CSS中没有意义,所以我没有把它包含在后面的版本:) –

+0

谢谢大家! :)你能帮我更新吗? –

1

输出我觉得应该是

module ApplicationHelper 
    def foo_link_to(text, path) 
    out = "<span class=\"span\">" 
    out += link_to text, path 
    out += "</span>" 
    out.html_safe 
    end 
end 
0

我想你应该在最后一行插入原始Øhtml_safe

0

html_safe可以帮助你:

使用out.html_safe

你可以参考this