![](/img/trans.png)
[英]How to convert html code to “html-converted” plain text with Ruby on Rails 4
[英]Ruby on Rails Helper Method - HTML is displayed as plain text
我為我的Rails應用程序提供了一個幫助方法,該方法返回一個包含Google Groups訂閱表單HTML代碼的字符串。 不幸的是,它出現在頁面上,就像純文本一樣。 如何強制它呈現為HTML? 提前致謝。
你的助手的結果需要在Rails 3中標記為“html_safe”。否則,標簽將被轉義。
def my_helper
data = "<p>Hello!</p>"
data.html_safe
end
我想這是Rails html sanitize的一個問題。 來自rails changelog
您不再需要調用h(字符串)來轉義HTML輸出,默認情況下它在所有視圖模板中都處於打開狀態。 如果你想要未轉義的字符串,請調用raw(字符串)。
試試吧
需要注意的一件事是加入這樣的多個字符串
def custom_check_box(checked)
'<span class="my-custom-checkbox '+( checked ? 'checked' : '')+'"></span>'.html_safe
end
在這種情況下,只有最后一部分被標記為html safe。 確保你得到了整個東西。
def custom_check_box(checked)
('<span class="my-custom-checkbox '+( checked ? 'checked' : '')+'"></span>').html_safe
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.