从 Rails 3 中的 lib 文件夹加载模块/类的最佳方法? [英] Best way to load module/class from lib folder in Rails 3?

查看:21
本文介绍了从 Rails 3 中的 lib 文件夹加载模块/类的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于最新的 Rails 3 版本不再从 lib 自动加载模块和类,加载它们的最佳方式是什么?

来自github:

<块引用>

这次提交做了一些改动:不要在 *lib* 中为应用程序自动加载代码(现在你需要明确地需要他们).这使得应用程序的行为更接近引擎(lib 中的代码仍然为插件自动加载);

解决方案

从 Rails 2.3.9 开始,在 config/application.rb 中有一个设置,您可以在其中指定包含要自动加载的文件的目录.

来自 application.rb:

# 包含要自动加载的类和模块的自定义目录.# config.autoload_paths += %W(#{config.root}/extras)

Since the latest Rails 3 release is not auto-loading modules and classes from lib anymore, what would be the best way to load them?

From github:

A few changes were done in this commit:

Do not autoload code in *lib* for applications (now you need to explicitly 
require them). This makes an application behave closer to an engine 
(code in lib is still autoloaded for plugins);

解决方案

As of Rails 2.3.9, there is a setting in config/application.rb in which you can specify directories that contain files you want autoloaded.

From application.rb:

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

这篇关于从 Rails 3 中的 lib 文件夹加载模块/类的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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