【发布时间】:2011-10-06 22:24:51
【问题描述】:
我有一个 Rails 项目,其中一个常量在服务请求时的某个时间点被核弹。
我正在使用 mime/types 和 restclient 宝石。 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 中显示的行中引发异常:
NoMethodError, message: undefined method `type_for_extension' for MIME::Types:Class
**这怎么可能? type_for_extension 与stringify_headers 在相同的文件 中定义;后者怎么会被核爆,而前者却不能?
编辑:已修复!
在我的配置中:
config.gem "aws-s3", :version => ">= 0.6.2", :lib => "aws/s3"
config.gem 'mime-types', :lib => 'mime/types'
aws-s3 正在通过require_library_or_gem 加载mime-types,最终调用了ActiveSupport::Dependencies.autoload_module!,该ActiveSupport::Dependencies.autoload_module! 维护了一个名为autoloaded_constants 的表,当ActionController.close 调用Dispatcher.cleanup_application 时,该表被nuked。
修复是先加载mime-types,所以它不会自动加载。
*呼*
【问题讨论】:
-
我想说您的编辑有资格作为答案。你可以回答自己的问题,我怀疑有人会反对回答这样棘手的问题。
-
@Mud 很高兴你能解决这个问题。
-
同意亩。请将您的解决方案添加为您的问题的答案,并将其标记为最佳答案。绝对允许回答您自己的问题:)
标签: ruby-on-rails ruby mime rest-client