How to autoload files in folder under rails app's root

耗尽温柔 提交于 2019-12-06 06:57:04

问题


I am trying to have files under myapplication/somefolder. Google and Stackoverflow say I should add this:

config.autoload_paths += %W(#{config.root}/somefolder)

in my config/application.rb, so I did.

But the files don't get loaded.

I tried namig somefolder/myclass.rb both class Myclass and class Somefolder::Myclass but still no luck.

I can see that the dir was found in Rails.application.config.autoload_paths in console does indeed include my /path/to/myapplication/somefolder directory, so that should be okay.

All the other questions around this topic use theapp/app/somefolder or theapp/lib/somefolder but not theapp/somefolder so maybe thats where it gets rotten.

So I tried referencing the class with ::Somefolder::MyClass but not even that helped.

I am using Rails 3.2.1


回答1:


Just ran into this myself today and decided to dive deep.

The reason you don't see in ActiveSupport::Dependencies.autoload_paths the paths you're adding to config.autoload_paths in config/application.rb is that they aren't copied over until the application is initialized. See rails/engine.rb in the railties gem:

module Rails
  class Engine < Railtie
    …

    # Set the paths from which Rails will automatically load source files,
    # and the load_once paths.
    #
    # This needs to be an initializer, since it needs to run once
    # per engine and get the engine as a block parameter
    initializer :set_autoload_paths, :before => :bootstrap_hook do |app|
      ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
      ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths)

      # Freeze so future modifications will fail rather than do nothing mysteriously
      config.autoload_paths.freeze
      config.eager_load_paths.freeze
      config.autoload_once_paths.freeze
    end

    …

    def _all_autoload_paths
      @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
    end

    …
  end
end

Were you by any chance trying to invoke MyClass from within config/application.rb or, even earlier, from a script or module that requires config/application.rb? If so, you'll have to explicitly require the file defining MyClass, e.g.:

require File.expand_path('../../somefolder/my_class',  __FILE__)
# now use MyClass



回答2:


The workaround is to go to ActiveSupport::Dependencies.autoload_paths directly.

ActiveSupport::Dependencies.autoload_paths << "#{config.root}/somefolder"

But I am still looking for the reason why config.autoload_paths didn't work so if you post an aswer to that, I'll accept it!




回答3:


You should name somefolder/my_class.rb in order to autoload MyClass. You also should keep that config.autoload_paths += %W(#{config.root}/somefolder) line in your config/application.rb.



来源:https://stackoverflow.com/questions/9430097/how-to-autoload-files-in-folder-under-rails-apps-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!