2011-07-16 53 views
13

我有一個Rails項目,其中一個常量在提供請求時在某個時刻被nuked。用Ruby/Rails解決「undefined constant」問題

我使用的是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舒拋出一個異常:

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_gemmime-types,其最終調用ActiveSupport::Dependencies.autoload_module!它保持的表稱爲autoloaded_constants這是核爆ActionController.close電話Dispatcher.cleanup_application

修復是首先加載mime-types,所以它不是自動加載的。

*咻*

+13

我想說你的編輯合格的答案。你被允許回答你自己的問題,我懷疑任何人都會反對這樣棘手的問題。 –

+0

@泥很高興你能解決這個問題。 – buruzaemon

+1

同意畝。請添加您的解決方案作爲您的問題的答案,並將其標記爲最佳答案。回答你自己的問題肯定是允許的:) – Emily

回答

3

被要求回答我的問題。

在我的配置:

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

aws-s3庫通過require_library_or_gem,最終調用ActiveSupport::Dependencies.autoload_module!它保持的表稱爲autoloaded_constants這是核爆時ActionController.close電話Dispatcher.cleanup_application.

修復程序加載MIME加載mime-types首先,它不是自動加載的。