English 中文(简体)
在Rails3中,我如何在控制器中使用mixin或模块?
原标题:How can I use mixins or modules in my controllers in Rails 3?

我在控制器中有一些行为,我把它们拉到一个模块中,以便更好地测试并在一些地方重用它。关于此,有两个问题:

  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?

谢谢各位。

最佳回答
  1. <code>lib/</code>是模块的绝佳场所;至少在我看来,这比<code>config/iinitializers/</code>要好得多。如果它是几个模块,或者是一个大模块,您也可以考虑将其作为插件,并将其放入vendor/plugins中。

  2. 如果将其放入lib/中,则需要手动需要文件。默认情况下,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) 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 ,  {**} )]

(我正在使用导轨3)

尝试将特定于控制器的模块放入应用程序/控制器中。无要求要求。





相关问题
How to use Maven 3 mixins?

I was trying to figure out how mixins are defined in Maven 3, but couldn t find anything other than buzz. It is propagated as one of the big new features here and here. I am currently feeling the pain ...

How do you access an instance variable within a mixin method?

How do you access an instance variable within a mixin method? I can think of 2 ways, but both seem problematic. Have the mixin method access the instance variable directly as any class method would, ...

Cannot access with_scope from a mixin

I just stumbled over a weird problem, and I don t really understand what is causing this. In our rails app, let s have a mixin Mixin: module Mixin def foo with_scope :find => ... do .....

Friend mixin template?

Let s say I have two classes Foo and Bar, and I want to make Foo friends with Bar without changing Foo. Here s my attempt: class Foo { public: Foo(){} private: void ...

scala: mixins depending on type of arguments

I have a set of classes of models, and a set of algorithms that can be run on the models. Not all classes of models can perform all algorithms. I want model classes to be able to declare what ...

Just-In-Time Derivation

There s a less common C++ idiom that I ve used to good effect a few times in the past. I just can t seem to remember if it has a generally used name to describe it. It s somewhat related to mixins, ...

热门标签