rails iframe标记未从文本中提取

时间:2016-08-29 04:14:12

标签: html ruby-on-rails iframe sanitize

我一直在努力将iframe标签嵌入到Ruby on Rails的页面中。这是我的所作所为:

  1. 管理员在textarea中输入带有标记的文字
  2. 使用保存按钮
  3. 保存到数据库
  4. 用户检索文本
  5. 显示文字
  6. 输入如下:

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

    它显示如下: the output of the text

    对于2和3,我已经确认标签实际上是在文本数据中保存和检索的。

    由于标签在视图中显示为功能标签,我认为不可能以这种方式嵌入标签,而且必须与安全问题有关。

    我尝试过(用haml):

    • = simple_format(@ product.description),
    • = simple_format(@ product.description,{},sanitize:false),
    • = sanitize(@ product.description),
    • = raw(@ product.description)

    ,但到目前为止,我没有运气。 (所有输出都相同)

1 个答案:

答案 0 :(得分:0)

这将解决您的问题:

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

这将调用@ product.description上的sanitize方法,并允许标记iframe和属性src,source,width和height在页面中呈现,而不是转义。

出于安全原因,默认情况下,Rails会捕获从数据库中提供的HTML。您将很快阅读有关交叉视觉脚本以及消毒宝石(上面使用过),但出于您的目的,您可以从this

开始

你的标记会有所不同,因为你正在使用haml。