Rails 3 中的库应该放在哪里? [英] Where should libraries go in Rails 3?

查看:35
本文介绍了Rails 3 中的库应该放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3 中库的推荐位置在哪里?像'lib'一样简单吗?

Where's the recommended location for libraries in Rails 3? Is it as simple as 'lib'?

我不确定,因为 'lib' 看起来更像是 Rails 2 的残余,特别是考虑到它不再自动加载(显然对此进行了很多讨论).

I'm not sure because 'lib' seems more like a Rails 2 remnant, especially considering that it's no longer auto-loaded (and there was a lot of discussion about that, apparently).

初始化器更多用于(显然)初始化任务,例如覆盖.

Initializers are more for (obviously) initialization tasks such as overrides.

具体来说,我有一个用于附件处理的小模块(Paperclip 不适合这里),它太大且不同,无法包含在我的模型中,但不够通用或不值得作为 gem 实现.

Specifically I have a small module for attachment handling (Paperclip doesn't fit here) that's too large and distinct to include in my model, but not generic or worthwhile enough to implement as a gem.

从功能的角度来看,它位于模型、视图和控制器之间的中间位置.这听起来像是一个助手,但在 Rails 中助手是为视图 AFAIK 设计的.

From a functionality standpoint it lives somewhere in the middle among the model, view, and controller. This makes it sound like a helper, but in Rails helpers are intended for views AFAIK.

我应该将它放在lib"中并自动加载到 application.rb 中吗?或者我可以创建一个自定义表单构建器来处理演示文稿(或两者).

Should I just put it in 'lib' and autoload it in application.rb? Or maybe I could create a custom form builder to handle the presentation (or both).

我知道如何让它发挥作用,但我希望能学到一些新东西.:)

I know how to make it work, but I'm hoping to learn something new. :)

推荐答案

lib 仍然是放置这些东西的正确位置.

lib is still the right place to put these kind of things.

由于引擎的工作方式,Rails 3 中删除了自动加载库,但主要是因为如果您确实希望它自动加载,则很容易将其添加到 autoload_paths 中,如果不想,您可以根据需要进行 require.lib 仍在加载路径中,因此您无需指定您需要的模块或类的位置.

Autoloading lib was removed in Rails 3 because of the way engines work, but mainly because it's easy to just add it to the autoload_paths if you do want it automatically loaded and if not, you can require as needed. lib is still in the load path, so you don't need to specify where the module or class you're requiring is.

您说得对,助手是为视图而设计的,而不是放置任何与模型相关的逻辑的地方.

You're correct, helpers are intended for the view, and would not be the place to put any model-related logic.

我会将模块放在 lib 中,并根据需要将其包含在您的模型中.

I'd put the module in lib, and require and include it in your model as needed.

这篇关于Rails 3 中的库应该放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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