代码之家  ›  专栏  ›  技术社区  ›  epochwolf

Rails:在库中使用ActionView方法sanitize时出错

  •  2
  • epochwolf  · 技术社区  · 15 年前

    线路 r_str = Helper.instance.sanitize(r_str, :tags => @@allowed_tags, :attributes => @@allowed_attribs) 是不是给了我错误

    undefined method `white_list_sanitizer' for Parsers::HTML::Helper:Class
    

    这是我的密码 lib/parsers.rb

    module Parsers
      module HTML
        @@allowed_tags = %w(--snip--)
        @@allowed_attribs = %w(--snip--)
    
        class Helper
            include Singleton
            include ActionView::Helpers::SanitizeHelper
        end
    
        #Use built-in santizer and the Hpricot plugin
        def self.clean(str)
          rgx = /<code>(.*?)<\/code>/ #All html within a code tag should be escaped.
          r_str = str.gsub(rgx) { |match| "<code>" + CGI.escapeHTML(match[5..-7]) + "</code>" } # TODO: test this.
          r_str = Helper.instance.sanitize(r_str, :tags => @@allowed_tags, :attributes => @@allowed_attribs)
          Hpricot(r_str)
        end
    
      end
    
      --snip-- 
    
    end
    

    3 回复  |  直到 15 年前
        1
  •  2
  •   Valters Vingolds jpkroehling    15 年前

    只需执行,而不是“包含ActionView::Helpers::SanitizeHelper”,

     include ActionView::Helpers
    

    上述内容将混合到SanitizeHelper提供的ClassMethods中,您的代码将正常工作。

    extend ActionView::Helpers::SanitizeHelper::ClassMethods
    
        2
  •  0
  •   Vitalie    15 年前

    您还需要来自sanitize helper的类方法

     class Helper
       include Singleton
       include ActionView::Helpers::SanitizeHelper
    
       class << self
         include SanitizeHelper::ClassMethods
       end
     end
    
        3
  •  -2
  •   epochwolf    15 年前

    rails中合适的类是 HTML::Sanitizer