2013-10-31 30 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