开发者

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

开发者 https://www.devze.com 2023-01-07 18:32 出处:网络
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).

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

精彩评论

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

关注公众号