Ruby-on-rails 为什么 Rails 不从应用程序/服务自动加载类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32873343/
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
Why doesn't Rails autoload classes from app/services?
提问by linkyndy
I'm working on a Rails 4.2 app and have just added app/services/fetch_artists.rbto the structure. Inside this file, I have defined a class FetchArtists; end.
我正在开发一个 Rails 4.2 应用程序,并且刚刚添加app/services/fetch_artists.rb到结构中。在这个文件中,我定义了一个class FetchArtists; end.
When trying to run rails r 'FetchArtists'it gives me a NameError: uninitialized constant FetchArtists.
尝试运行时,rails r 'FetchArtists'它给了我一个NameError: uninitialized constant FetchArtists.
I've tried looking at ActiveSupport::Dependencies.autoload_pathsand indeed, app/servicesis not there:
我试过看,ActiveSupport::Dependencies.autoload_paths确实app/services不存在:
/.../app/assets
/.../app/controllers
/.../app/helpers
/.../app/jobs
/.../app/mailers
/.../app/models
/.../app/controllers/concerns
/.../app/models/concerns
/.../spec/mailers/previews
My question is, why isn't this folder automatically loaded, and what should I do for it to be?
我的问题是,为什么不自动加载这个文件夹,我应该怎么做?
EDIT
编辑
Very strange, after repeatedly running the above command with rails runner, the new folder appears on the autoload paths. I have no idea why this happened with such a lag.
很奇怪,用 重复运行上述命令后rails runner,新文件夹出现在自动加载路径上。我不知道为什么会发生这样的滞后。
Someone suggested this may deal with spring. I would like to hear more on this, since it can possibly help many others in this situation too.
有人建议这可以处理spring。我想听到更多关于这方面的信息,因为它也可能在这种情况下帮助许多其他人。
回答by maknz
I encountered the same problem and it seems to be a caching issue with Spring, a process which handles preloading your app. It's used for the web server as well as the console and Rake tasks.
我遇到了同样的问题,它似乎是 Spring 的缓存问题,这是一个处理预加载应用程序的过程。它用于 Web 服务器以及控制台和 Rake 任务。
Stopping Spring with bin/spring stopwill force Spring to load your app fresh. Now running rails consoleand inspecting ActiveSupport::Dependencies.autoload_pathswill successfully show app/services.
停止 Spring withbin/spring stop将强制 Spring 重新加载您的应用程序。现在运行rails console和检查ActiveSupport::Dependencies.autoload_paths将成功显示app/services.
回答by Brian
In my case spring was not watching the app/servicesdirectory for changes - restarting Spring would load the class but changes to an existing class or new class would require a restart of Spring for them to apply.
在我的情况下,spring 没有监视app/services目录的更改 - 重新启动 Spring 会加载类,但对现有类或新类的更改需要重新启动 Spring 才能应用它们。
To resolve this issue I added it to the list of directories watched by Spring in config/spring.rb:
为了解决这个问题,我将它添加到 Spring 监视的目录列表中config/spring.rb:
%w(
.ruby-version
.rbenv-vars
tmp/restart.txt
tmp/caching-dev.txt
app/services
).each { |path| Spring.watch(path) }
and restarted Spring one more time.
并再次重新启动Spring。
回答by sbauch
I came with a similar problem, and took a quick glance at the Spring docs and found this bit about watchers.
我遇到了类似的问题,快速浏览了 Spring 文档,发现了关于watchers 的这一点。
I added the following to my application.rb and things fell into place -
我将以下内容添加到我的 application.rb 中,事情就到位了 -
Spring.watch "app/services/**"
I'm no expert here, ymmv.
我不是这里的专家,ymmv。
回答by Daniel R Carletti
I was having the same problem, and found no solution. I'm not patient enough to wait for autoload to load it eventually, so my quick solution was to turn eager_load on, and start my server. It will finally load it. I switched it off afterwards and my classes were still loaded.
我遇到了同样的问题,没有找到解决方案。我没有足够的耐心等待自动加载最终加载它,所以我的快速解决方案是打开eager_load,然后启动我的服务器。它最终会加载它。后来我把它关掉了,我的课还在加载。
Just use:
config.eager_load = true
只需使用:
config.eager_load = true
in your config/environments/development.rb
在你的 config/environments/development.rb
回答by dimakura
You should include it into autoload_paths:
您应该将其包含在autoload_paths:
config.autoload_paths += %W(#{Rails.root}/app/services)

