Hpricot - nil的未定义方法`to_sym':NilClass

时间:2011-05-17 18:29:05

标签: ruby-on-rails ruby-on-rails-3 hpricot

我最近做了一个捆绑更新,结果破坏了很多东西。我无法解决的一件事是使用hpricot来设置文本格式。

任何hpricot用户以前都会因为nilClass错误而得到这个to_sym?感谢

错误:

ActionView::Template::Error (undefined method `to_sym' for nil:NilClass):
config/initializers/hpricot_text_transform.rb:11:in `text_transform!'

配置/初始化/ hpricot_text_transform.rb

# By Henrik Nyh <http://henrik.nyh.se> 2007-03-28.
# Based on http://vemod.net/code/hpricot_goodies/hpricot_text_gsub.rb.
# Licensed under the same terms as Ruby.

require "rubygems"
require "hpricot"

module HpricotTextTransform
  module NodeWithChildrenExtension
    def text_transform!(options={}, &block)
      return if defined?(name) and Array(options[:except]).include?(name.to_sym)
      children.each { |c| c.text_transform!(options, &block) }
    end
  end

  module TextNodeExtension
    def text_transform!(options={}, &block)
      content.replace yield(content)
    end
  end

  module BogusETagExtension
    def text_transform!(options={}, &block)
    end
  end
end

Hpricot::Doc.send(:include,  HpricotTextTransform::NodeWithChildrenExtension)
Hpricot::Elem.send(:include, HpricotTextTransform::NodeWithChildrenExtension)
Hpricot::BogusETag.send(:include, HpricotTextTransform::BogusETagExtension)
Hpricot::Text.send(:include, HpricotTextTransform::TextNodeExtension)

1 个答案:

答案 0 :(得分:0)

尝试替换

return if defined?(name) and Array(options[:except]).include?(name.to_sym)

return if defined?(name) and !(name.nil?) and Array(options[:except]).include?(name.to_sym)