多个控制器和模型共享的代码——保存它的最佳位置在哪里?
所以这是一个新手导轨设计问题。假设我希望一些常用功能位于一组辅助类中(作为类方法或实例方法)。
我想在控制器(而不是视图)内使用这些辅助对象,甚至可能是一个模型。我可以这样做吗?如何?它必须是模块或类还是可以是任何东西?
Rails 有专门的模式吗?
So this is a newbie rails design question. Lets say I want some of my common functionality to be sitting in a set of helper classes (either as class methods or instance methods).
And I want to use these helper objects inside controller (not view) or even may be a model. can I do that? how? Does it have to be a module or class or can be anything?
Is there rails specific pattern for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它们未绑定到三层之一,则应将它们放置在 /lib 目录中。
/lib 下的约定是您应该将文件夹命名为模块、文件和类,并且您应该始终尝试将其他行为封装在模块中。假设您有一些类,
您应该将其放入 /lib/my_module/my_helper_class.rb
If their are not tied to one of the three tiers, you should place them in the /lib directory.
The convention under /lib is that you should name your folders as modules, and files and classes, and that you should always try to encapsulate your additional behavior in modules. Let's say, you have some class
You should put it into /lib/my_module/my_helper_class.rb