user938363
user938363

Reputation: 10378

Where to put methods which deem to be shared for whole app?

By default, rails app shares all helper methods for views. Any helper methods is automatically shared among all views and this may cause naming conflict or confusing if the application gets big. There are some methods which deem to be shared for the whole app. How to share and where to put those "global" methods?

Thanks.

Upvotes: 0

Views: 231

Answers (1)

mliebelt
mliebelt

Reputation: 15525

I think there is normally no need for "global" methods, due to the fact that Rails is based on Ruby which is an object-oriented language. Rails is implementing MVC in a strong way which is an object-oriented pattern as well.

But if you need to place code that is globally accessible, you are free to use Ruby that allows that by extending any class. So by using

class Object
   def my_global_method
     ...
   end
end

this will be available everywhere if loaded. Reading the "Rails Guide on Configuration" I think the natural place is to require the file you have added in config/application.rb. Another option could be to place the file in the directory config/initializers, so it will be loaded automatically after Rails is initialized.

Upvotes: 2

Related Questions