如何在 Rails 3 的控制器中使用 mixin 或模块?

发布于 2024-09-27 19:59:45 字数 239 浏览 4 评论 0原文

我的控制器中有一些行为,我将其提取到模块中,以便更好地测试并在一些地方重复使用它。关于此的两个问题:

  1. 哪里是放置我的模块的好地方?它们需要运行才能可供控制器使用,所以我想到了 config/initializers/ 目录。不过,这对我来说似乎有点可疑。 lib/
  2. 如何确保代码运行,以便模块可包含到我的控制器中?

先生们,谢谢你们。

I have some behavior in my controller that I pulled out into a module in order to test better and re-use it in a few places. Two questions about this:

  1. Where is a good place to put my modules? They need to run in order to be available to the controllers, so I was thinking the config/initializers/ directory. That seems a little suspect to me though. lib/?
  2. How do I ensure the code gets run so the modules are available to include in my controllers?

Thank you kindly sirs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴者 2024-10-04 19:59:45
  1. lib/ 是放置模块的绝佳位置;比 config/initializers/ 好得多——至少在我看来。如果它是多个模块,或者是一个大模块,您还可以考虑将其制作为一个插件并将其放置在 vendor/plugins 中。

  2. 如果将其放入 lib/ 中,则需要手动 require 该文件。默认情况下,Rails 不会自动加载 lib/ 目录中的文件。您可以将 require 放入您的配置文件之一。

我通常将额外的自动加载放在 config/application.rb 中。像这样的事情应该可以解决问题(假设您的 .rb 文件位于名为 lib/my_module 的目录中):

config.autoload_paths += Dir["#{Rails.root}/lib/my_module"]

您必须确保您的模块是实际的 <代码>模块而不是<代码>类。然后,您可以简单地包含它:

# lib/my_module/foobar.rb
module Foobar
  def foobar
    "Hello world!"
  end
end

# app/models/my_model.rb
class MyModel < ActiveRecord::Base
  include Foobar
end

# rails console
>> obj = MyModel.first
=> #<MyModel id: 1, ...>
>> obj.id
=> 1
>> obj.foobar
=> "Hello world!"
  1. lib/ is an excellent place for modules; much better than config/initializers/--at least in my opinion. If it's several modules, or one large one, you can also consider making it a plugin and placing it in vendor/plugins.

  2. If you put it in lib/, you'll need to manually require the file. Rails, by default, does not autoload files in the lib/ directory. You can place the require in one of your config files.

I usually put my additional autoloads in config/application.rb. Something like this should do the trick (assuming that your .rb file is in a directory called lib/my_module):

config.autoload_paths += Dir["#{Rails.root}/lib/my_module"]

You have to make sure that your module is an actual module and not a class. Then, you can simply include it:

# lib/my_module/foobar.rb
module Foobar
  def foobar
    "Hello world!"
  end
end

# app/models/my_model.rb
class MyModel < ActiveRecord::Base
  include Foobar
end

# rails console
>> obj = MyModel.first
=> #<MyModel id: 1, ...>
>> obj.id
=> 1
>> obj.foobar
=> "Hello world!"
情魔剑神 2024-10-04 19:59:45

1)我喜欢这样写:
我的类扩展在 app/extensions 下
我的模块位于 /app/mixins 下
我的服务位于 /app/services 下

2) 您可以配置应用程序以在 config/application.rb 中加载所有这些内容:
应该以正确的方式要求类扩展
并且 mixins 和服务可以添加到您的自动加载路径中

  class Application < Rails::Application
    # require class extentions right now
    Dir[Rails.root.join('app', 'extentions', "*.rb")].each {|l| require l }

    # Custom directories with classes and modules you want to be autoloadable.
    config.autoload_paths += Dir[Rails.root.join('app', 'mixins', '{**}')]
    config.autoload_paths += Dir[Rails.root.join('app', 'services', '{**}')]

(我使用的是 Rails 3)

1) I like to put:
my class extentions under app/extentions
my modules under /app/mixins
my services under /app/services

2) You can configure your application to load all of these in config/application.rb:
class extentions should be required right way
and the mixins and services can be added to your autoload path

  class Application < Rails::Application
    # require class extentions right now
    Dir[Rails.root.join('app', 'extentions', "*.rb")].each {|l| require l }

    # Custom directories with classes and modules you want to be autoloadable.
    config.autoload_paths += Dir[Rails.root.join('app', 'mixins', '{**}')]
    config.autoload_paths += Dir[Rails.root.join('app', 'services', '{**}')]

(I'm using rails 3)

夏末 2024-10-04 19:59:45

尝试将控制器特定模块放入 app/controllers 中。不需要require

Try putting controller specific modules in app/controllers. No require required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文