开发者

Code shared by multiple controllers and models -- where is the best place to keep it?

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 instanc开发者_如何学JAVAe 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜