使用Ruby / Rails解决“未定义的常量”问题

时间:2011-07-16 22:32:41

标签: ruby-on-rails ruby mime rest-client

我有一个Rails项目,在服务请求时,某个常量在某个时刻被修复。

我正在使用mime/typesrestclient宝石。 restclient模块定义了MIME的扩展名,其中包含方法type_for_extension

module RestClient
    ...
    def stringify_headers headers
      result[key] = target_values.map { |ext| MIME::Types.type_for_extension(ext.to_s.strip) }.join(', ')
      ...
    end
  end
end

module MIME
  class Types
    def type_for_extension ext
      candidates = @extension_index[ext]
      candidates.empty? ? ext : candidates[0].content_type
    end
    class << self
      def type_for_extension ext
        @__types__.type_for_extension ext
      end
    end
  end
end

我可以在给定控制器操作的第一次调用上访问MIME::Types.type_for_extension。在第二次调用时,它已经消失了。

我仍然可以使用MIME::Types.type_for,但添加的方法已经消失了,所以当我尝试使用RestClient模块时,会在stringify_headers的showin行引发异常:

NoMethodError, message: undefined method `type_for_extension' for MIME::Types:Class

**这怎么可能? {em>同一文件中定义的type_for_extensionstringify_headers;后者怎么会受到伤害而不是前者呢?


编辑:修复它!

在我的配置中:

config.gem "aws-s3", :version => ">= 0.6.2", :lib => "aws/s3"  
config.gem 'mime-types', :lib => 'mime/types'

aws-s3正在通过mime-types加载require_library_or_gem,最终调用了ActiveSupport::Dependencies.autoload_module!,其中维护了一个名为autoloaded_constants的表 nuked ActionController.close调用Dispatcher.cleanup_application时。

修复是首先加载mime-types,因此它不会自动加载。

*噢*

1 个答案:

答案 0 :(得分:3)

按要求回答我自己的问题。

在我的配置中:

config.gem "aws-s3", :version => ">= 0.6.2", :lib => "aws/s3"  
config.gem 'mime-types', :lib => 'mime/types'

aws-s3库正在通过mime-types加载require_library_or_gemActiveSupport::Dependencies.autoload_module!最终调用了autoloaded_constants,其中维护了一个名为ActionController.close的表,该表在Dispatcher.cleanup_application.时被核化致电{{1}}

修复是先加载mime-types,所以它不是自动加载的。

相关问题