Ruby-on-rails 将目录添加到 Rails 中的加载路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1223481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:35:09  来源:igfitidea点击:

Adding a directory to the load path in Rails?

ruby-on-railsruby

提问by pjb3

As of Rails 2.3, what's the right way to add a directory to the load path so that it hooks into Rails' auto-reloading mechanisms?

从 Rails 2.3 开始,将目录添加到加载路径以使其挂钩到 Rails 的自动重新加载机制的正确方法是什么?

The specific example I'm thinking of is I have a class that has several sub-classes using STI and I thought it would be a good idea to put them in a sub-directory rather than clutter the top-level. So I would have something like:

我想到的具体例子是我有一个类有几个使用 STI 的子类,我认为将它们放在子目录中而不是将顶级弄乱是个好主意。所以我会有类似的东西:

#app/models/widget.rb
class Widget < ActiveRecord::Base
   add_to_load_path File.join(File.dirname(__FILE__), "widgets")
end

#app/models/widgets/bar_widget.rb
class BarWidget < Widget
end

#app/models/widgets/foo_widget.rb
class FooWidget < Widget
end

It's the add_to_load_pathmethod that I'm looking for.

add_to_load_path是我正在寻找的方法。

回答by Jamel Toms

In the current version of Rails (3.2.8), this has been changed in the application.rb file.

在当前版本的 Rails (3.2.8) 中,这已在 application.rb 文件中进行了更改。

The code is currently commented out as:

该代码目前被注释为:

  # Custom directories with classes and modules you want to be autoloadable.
  # config.autoload_paths += %W(#{config.root}/extras)

Will need to update the autoload_pathsvalue. Attempting to modify the the former load_pathsvariable causes this error.

将需要更新autoload_paths值。尝试修改之前的load_paths变量会导致此错误。

/configuration.rb:85:in `method_missing': undefined method `load_paths' for #<Rails::Application::Configuration:0xac670b4> (NoMethodError)

for an example, for each path to add to autoload_paths config, add a line similar to the following:

例如,对于要添加到 autoload_paths 配置的每个路径,添加类似于以下内容的行:

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


config.autoload_pathsaccepts an array of paths from which Rails will autoload constants. Default is all directories under app.

config.autoload_paths接受 Rails 将自动加载常量的路径数组。默认为app.

http://guides.rubyonrails.org/configuring.html

http://guides.rubyonrails.org/configuring.html



From commentor (hakunin) below:

来自以下评论员(hakunin):

If the directory is under app/, you don't need to add it anywhere, it should just work by default (definitely in 3.2.12). Rails has eager_load_pathsthat acts as autoload_pathsin development, and eager load in production. All app/*directories are automatically added there.

如果目录在 下app/,则无需将其添加到任何地方,默认情况下它应该可以工作(肯定在 3.2.12 中)。Rails在开发eager_load_paths中扮演着这样autoload_paths的角色,在生产中扮演着急切加载的角色。所有app/*目录都会自动添加到那里。

回答by ryanb

For older versionsof Rails:

对于旧版本的 Rails:

You can do this in your environment.rb config file.

您可以在您的 environment.rb 配置文件中执行此操作。

config.load_paths << "#{RAILS_ROOT}/app/widgets"

--

——

For Rails 3, see answers bellow

对于Rails 3,请参阅下面的答案

回答by Jacob

In Rails 3, you can set this in config/application.rb, where this sample is provided by default:

在 Rails 3 中,您可以在 config/application.rb 中进行设置,这里默认提供此示例:

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{config.root}/extras )

回答by Peter Piper

In Rails 5you don't have to explicitly load folders from within the app directory anymore. All folders placed inside are directly available. You don't have to touch any of the config files. But it seems as if there are some issues with Spring.

Rails 5 中,您不再需要从 app 目录中显式加载文件夹。放在里面的所有文件夹都可以直接使用。您不必接触任何配置文件。但似乎 Spring 存在一些问题。

The new workflow therefore is:

因此,新的工作流程是:

  1. create a new folder and class inside the /app directory
  2. run spring stopon the command line
  3. check the autoload-paths with bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'on the command line. The new folder should now be listed.
  4. run spring starton the command line
  1. 在 /app 目录中创建一个新文件夹和类
  2. spring stop在命令行上运行
  3. bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'在命令行上检查自动加载路径。现在应该列出新文件夹。
  4. spring start在命令行上运行

回答by Tiago Franco

On Rails 5 you need to add the following code to environment.rb:

在 Rails 5 上,您需要将以下代码添加到 environment.rb:

# Add the widgets folder to the autoload path
Rails.application.configure do
  config.autoload_paths << "#{Rails.root}/app/widgets"
end

回答by gdakram

Another update for rails 3 -- activesupport 3.0.0:

rails 3 的另一个更新——activesupport 3.0.0:

Instead of:

代替:

ActiveSupport::Dependencies.load_paths << "#{RAILS_ROOT}/app/widgets"

You may need to do this:

您可能需要这样做:

ActiveSupport::Dependencies.autoload_paths << "#{RAILS_ROOT}/app/widgets"

回答by Lewis Hoffman

I found I needed to do this after config block-- no access to config object anymore.

我发现我需要在配置块之后执行此操作——不再访问配置对象。

This did the trick

这成功了

ActiveSupport::Dependencies.load_paths << "#{RAILS_ROOT}/app/widgets"

回答by Tyler

In config/application.rbadd config.autoload_paths << "#{config.root}/models/widgets".

config/application.rb添加config.autoload_paths << "#{config.root}/models/widgets".

File should look like this:

文件应如下所示:

module MyApp
  class Application < Rails::Application
    config.autoload_paths << "#{config.root}/models/widgets"
  end
end

I know this works for Rails 4 and 5. Probably others as well.

我知道这适用于 Rails 4 和 5。可能也适用于其他人。