2016-08-29 13 views
0

我一直在努力將iframe標籤嵌入到Ruby on Rails的頁面中。這是我做的:在textarea的具有標籤在它rails iframe標籤不是從文本中提取

  1. 管理員輸入文本
  2. 保存到與保存按鈕數據庫
  3. 用戶檢索文本
  4. 顯示文本

輸入如下:

aaaaaaaaaaaaa 
<iframe width="560" height="315" src="https:// 
www.youtube.com/embed/Z4jib_xBTtY" frameborder="0"></iframe> 
bbbbbbbbbbbbb 

它dis打這樣的: the output of the text

對於2和3,我已確認該標記實際上是保存和檢索文本數據。

由於標籤在視圖中顯示爲功能標籤,因此我認爲不可能以這種方式嵌入標籤,而且它必須與安全問題有關。

我試圖(與HAML):

  • = simple_format(@ product.description),
  • = simple_format(@ product.description,{},消毒:假),
  • =的sanitize(@ product.description)
  • =原(@ product.description)

,但到目前爲止,我已經沒有運氣。 (都具有相同的輸出上面放)

+0

你想把文字放在哪裏?您是否嘗試在html標籤內使用''<%= your_text%?'''。 – Nathan

回答

0

這將解決您的問題:

<%= sanitize @product.description, tags: %w(iframe), attributes: %w(src source width height %> 

此呼籲@ product.description的方法sanitize方法,並允許標籤iframe和屬性SRC,來源,寬度以及要在頁面中呈現的高度,而不是轉義。

出於安全原因,Rails會從數據庫中提取HTML。您需要儘快閱讀交叉視線腳本,以及清理寶石(以上使用),但出於您的目的,您可以從這開始this

由於您使用的是haml,因此您的標記會有點不同。

相關問題