Is it a good idea to leave helper :all in the applications_controller?
I am working for a company where I inherited their administration site. The guy before me didn't use view helpers much so 95% of the code in these helpers is mine.
I just got bit by a problem where he had the helper :all line in his application_controller, which means all helpers are available to all views. I got bit because I created the same named method in two different helpers and of course it took the last one instead of the correct one. It drove me nuts wondering how a view helper that had NOTHING to do with the controller I was in was being loaded and the wrong method being selected.
My question is, is this a good idea to leave in your application controller? I would think that the standard would be to ONLY include the view helper for THAT particular view's controller. So if I have foo controller and foo views, only the foo_helper should be included. Not foo2_helper and foo3_helper. (I know if I were to need foo2_helper or foo3_helper you can put "helper foo2" or "helper foo3" in the controller, but that would be by choice, not automatic.)
I can't seem to find an answer to if this is a good idea开发者_运维百科 or not. I found one site that said this is the default, really??
Thanks
IMO helpers in the ApplicationController
are specifically meant to be shared across the application (which I guess is kind of obvious when I say it out loud).
Helpers used only in a single controller's views belong in that single controller.
If there are non-global helpers in the application controller they're likely in the wrong spot.
I keep helper :all
in my ApplicationController, because I order my helpers depending of what I use them to, not depending of the controller.
That way, you can reuse the same helper in two different controllers.
Yes. It's called DRY.
It doesn't mean you should place all your code in application_helper.rb
and use it in every place of your rails project, but it can help sometimes.
精彩评论