I need to have the exact same method available in all controllers and
in all views. Right now, I have it repeated in app/controllers/
application_controller.rb and app/helpers/application_helper.rb. In
DRY tradition, what's the best way to only have it in one place?
TIA,
Craig
Just put it in a helper and include that helper in ApplicationController like this
include MyHelperWithGlobalStuffInIt
You're right, putting
include ApplicationHelper
into application_controller.rb does the trick.
But wasn't that what
helper :all # include all helpers, all the time
was supposed to do?
Or do I misunderstand?
I’ve wondered about that too. That doesn’t work for me either. It doesn’t raise an error but doesn’t include :all either.
You do :-). The helper method controls which of your helpers (in app/
helpers) are available to the views for that controller. Without the
helper :all call, by default views rendered by foo_controller will get
helper methods from foo_helper. You can name specific modules, so for
example if you a bunch of common helpers in app/helpers/
common_helpers.rb then you say helper :common_helpers or helper
CommonHelpers to add that module for the controller in question.
helper :all means add all of the helpers from app/helpers to all of
your views.
Another way of solving the original problem is declaring the method in
application_controller and then saying
helper_method :some_helper
which takes the named controller method and makes it into a view
helper.
Fred
> Or do I misunderstand?
You do :-). The helper method controls which of your helpers (in app/
helpers) are available to the views for that controller.
Ahhhhh. So *that's* what it's there for!
Another way of solving the original problem is declaring the method in
application_controller and then sayinghelper_method :some_helper
which takes the named controller method and makes it into a view
helper.
Thanks!
Craig