【问题标题】:Allow id attributes with simple_format helper允许带有 simple_format 帮助器的 id 属性
【发布时间】:2011-07-27 17:44:44
【问题描述】:

作为概念证明,这里首先是一些控制台输出:

ruby-1.9.2-p180 :010 > x = "<span id='c_3'>s</span>"
 => "<span id='c_3'>s</span>" 
ruby-1.9.2-p180 :011 > helper.simple_format(x)
 => "<p><span>s</span></p>"

原因是 Rails 辅助方法 simple_format 在执行的最后调用了 sanitize 方法,并且该方法去除了属性。

我知道sanitize 将允许您指定不应被剥离的属性。我的问题是:是否有可能通过 simple_format 以某种方式传递“白名单”属性(在这种情况下为 id)?

谢谢!!

【问题讨论】:

    标签: ruby-on-rails views html-helper


    【解决方案1】:
    simple_format(x,{}, :sanitize => false)
    

    【讨论】:

      【解决方案2】:

      你不能通过白名单,但你可以通过这样做完全禁用清理

      simple_format(x, :sanitize => false)
      

      http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

      【讨论】:

      • 啊!我现在明白了。查看源代码,我看到这一行` text = sanitize(text) unless options[:sanitize] == false` 完全点击。非常感谢@Dogbert!
      • 实际上,要禁用清理,您需要使用 第三个 参数,而不是第二个参数,即“html 选项”,另一个哈希。所以正确的形式是
        simple_format(x, {}, :sanitize =&gt; false)
      猜你喜欢
      • 2014-05-23
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多