2016-03-01 66 views
0

我在我的意見中使用了一些翻譯。這些翻譯有時在他們返回非常基本的HTML標記 -從翻譯字符串中剝離HTML標記

t("some.translation") 
#=> "This is a translation <a href="/users">with some</a> markup<br />" 

(附註:我使用的是夢幻般的it gem輕鬆嵌入標記,具體環節,我的翻譯)

如果我想要什麼在某些情況下剝離HTML標籤,例如當我在RSpec測試中使用翻譯字符串時。是否有一個HTML strp功能可以編譯和刪除該標記?

t("some.translation").some_html_strip_method 
#=> "This is a translation with some markup" 

謝謝!

回答

4

你可能要嘗試從::的ActionView助手strip_tags :: SanitizeHelper

strip_tags("Strip <i>these</i> tags!") 
# => Strip these tags! 

strip_tags("<b>Bold</b> no more! <a href='more.html'>See more here</a>...") 
# => Bold no more! See more here... 

strip_tags("<div id='top-bar'>Welcome to my website!</div>") 
# => Welcome to my website! 
0

根據你使用它。

用strip_tags方法不控制器模型,或

它有錯誤出現約

功能white_list_sanitizer在你使用它的類定義。

爲了解決這個問題,使用

ActionController::Base.helpers.strip_tags('string') 

爲了縮短這一點,添加像這樣在初始化:

class String 
    def strip_tags 
    ActionController::Base.helpers.strip_tags(self) 
    end 
end 

然後用稱之爲:

'string'.strip_tags 

但是如果你只需要在視圖中使用它 ,簡單地說:

<%= strip_tags(t("some.translation")) %>