2009-06-18 35 views
2

我正在尝试使用ActionView中的Sanitize方法。Rails:使用ActionView方法清理Lib中的错误

线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 

代码我在做什么错?

(请不要允许用户提交HTML的危险性发表意见,我所知道的风险)

回答

-1

在轨道的正确类是HTML::Sanitizer

+2

我不知道为什么选择这个答案......这些都不工作了我。 – bchurchill 2013-02-20 21:10:29

0

您还需要的sanitize辅助类方法

class Helper 
    include Singleton 
    include ActionView::Helpers::SanitizeHelper 

    class << self 
    include SanitizeHelper::ClassMethods 
    end 
end 
1

根本就不是“包括的Act​​ionView: :助手:: SanitizeHelper”,

include ActionView::Helpers 

这上面会从SanitizeHelper的ClassMethods混合,你的代码将工作。

注:我也看到了建议明确地做:

extend ActionView::Helpers::SanitizeHelper::ClassMethods 
相关问题