如何列出Rails 3中的所有自动加载路径?如何列出Rails 3中的所有自动加载路径
在Rails控制台当我这样做,它只是列出添加到配置自定义路径:
$ rails c
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > MyRailsApp::Application.config.autoload_paths
=> []
如何列出Rails 3中的所有自动加载路径?如何列出Rails 3中的所有自动加载路径
在Rails控制台当我这样做,它只是列出添加到配置自定义路径:
$ rails c
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > MyRailsApp::Application.config.autoload_paths
=> []
更新:请通过下面的ActiveSupport :: Dependencies.autoload_paths看到劳拉的答案。我在这里留下了这个答案作为替代方法。
在Rails::Engine
其中包括Rails应用程序的模块中,有以下方法:
def _all_autoload_paths
@_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
end
所以,你既可以做:
(MyRailsApp::Application.config.autoload_paths + MyRailsApp::Application.config.eager_load_paths + MyRailsApp::Application.config.autoload_once_paths).uniq
或:
[:autoload_paths, :eager_load_paths, :autoload_once_paths].collect{|m|MyRailsApp::Application.config.send(m)}.flatten.uniq
或只是:
MyRailsApp::Application._all_autoload_paths
在导轨3.2.9默认结果是:
["/path/to/my_rails_app/app/assets", "/path/to/my_rails_app/app/controllers", "/path/to/my_rails_app/app/helpers", "/path/to/my_rails_app/app/mailers", "/path/to/my_rails_app/app/models"]
这应包括由其他的宝石和定制负载路径添加的所有自动加载路径。
您可以通过ActiveSupport::Dependencies.autoload_paths
访问所有的自动加载路径从控制台调用或从命令行运行rails r 'puts ActiveSupport::Dependencies.autoload_paths'
。
这里更多的信息(对于轨道4,但它适用到Rails 3为好): http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths
作品在导轨5以及 – dps