如何在Rails应用程序根目录下的文件夹中自动加载文件 [英] How to autoload files in folder under rails app's root

查看:104
本文介绍了如何在Rails应用程序根目录下的文件夹中自动加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件保存在myapplication/somefolder下. Google和Stackoverflow说我应该添加这个:

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

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

在我的config/application.rb中是

,所以我做到了.

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

但是文件没有被加载.

我尝试了class Myclassclass Somefolder::Myclass的namig somefolder/myclass.rb,但还是没有运气.

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

我可以看到在控制台的Rails.application.config.autoload_paths中找到的目录确实包含我的/path/to/myapplication/somefolder目录,因此应该可以.

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.

围绕该主题的所有其他问题都使用theapp/app/somefoldertheapp/lib/somefolder而不是theapp/somefolder,所以也许那是烂的.

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.

所以我尝试用::Somefolder::MyClass引用该类,但没有帮助.

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

我正在使用Rails 3.2.1

I am using Rails 3.2.1

推荐答案

今天我自己也碰上了这个,决定下潜.

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

ActiveSupport::Dependencies.autoload_paths中看不到要添加到config/application.rb中的config.autoload_paths中的路径的原因是,在初始化应用程序之前,它们不会被复制.请参见railties宝石中的rails/engine.rb:

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

您是不是偶然地尝试从config/application.rb内部甚至更早地从需要config/application.rb的脚本或模块中调用MyClass?如果是这样,您将必须明确要求定义MyClass的文件,例如:

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

这篇关于如何在Rails应用程序根目录下的文件夹中自动加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆