views:

40

answers:

1

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?

Thanks for your help.

A: 

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

module MyModule

  class MyHelperClass
  end

end

You should put it into /lib/my_module/my_helper_class.rb

Chubas
If you don't follow this convention, you will have to manually load them (see http://litanyagainstfear.com/blog/2008/09/22/loading-custom-code-in-rails/). The MyHelperClass will be available for both; just make sure you load it with its full name (MyModule::MyHelperClass) or include the module MyModule in your classes.
Chubas