默认情况下不查找 lib 目录 [英] lib directory is not looked in by default
问题描述
我遵循 Ryan 的子域 railscast,其中 het 创建了一个使用的子域类在他的路线中并将其放在 lib 目录中.显然,我的 rails 应用程序默认不会在 lib 目录中查找(我的应用程序仅在我将 subdomain.rb 文件从 lib 移动到例如/app/models 时才启动)
I following Ryan's subdomain railscast where het creates a Subdomain class used in his routes and places it in the lib directory. Apparantly my rails app isn't looking in the lib dir by default (my app only starts when I move the subdomain.rb file from lib to eg /app/models)
我一直认为 lib 目录默认包含在 rails 应用程序中?我怎样才能最好地包含这个目录来实现这一点.
I always thought that the lib dir was included by default in a rails app? How can I best include this directory to make this happen.
谢谢
推荐答案
Rails 3 默认不会自动加载它(尽管 Rails 2 会).您必须将其添加到 application.rb 中的 autoload_paths.
Rails 3 does not autoload it by default (although Rails 2 does). You have to add it to your autoload_paths in application.rb.
module YourRailsApp
class Application < Rails::Application
config.autoload_paths += %W(#{Rails.root}/lib)
#...
end
end
有关背后原因的更多信息,请参阅:
For more info on the reason behind this see:
https://rails.lighthouseapp.com/projects/8994/tickets/5218-rails-3-rc-does-not-autoload-from-lib
这篇关于默认情况下不查找 lib 目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!