The Pug Automatic

Don't include all helpers

Written August 30, 2012. Tagged Ruby on Rails.

In Rails, all helpers are included in all views by default.

So your UsersHelper methods are also available to your ItemsController views.

I don't like having that much stuff in the same namespace. Method names may collide, or else need inelegant prefixes.

This setting makes things more sensible:

config/application.rb
config.action_controller.include_all_helpers = false

ApplicationHelper is still included in all views. Admin::UsersHelper is only included in Admin::UsersController views. If that controller inherits from Admin::BaseController, then Admin::BaseHelper will be included as well.

And here's a nice (and fairly obvious) trick: you can still use regular Ruby modules to organize your methods into more than one file.

For example, I like to have a LayoutHelper that the application layout template can use:

app/helpers/layout_helper.rb
module LayoutHelper
def some_layout_helper_method
"Flappity flippity."
end
end
app/helpers/application_helper.rb
module ApplicationHelper
include LayoutHelper

def some_global_helper_method
"Hippity wippity."
end
end

With include_all_helpers off, the LayoutHelper isn't automatically included anywhere. But by simply includeing it in the global ApplicationHelper, now its methods are global too.

You get the benefits of splitting helpers into multiple files, without the downsides of having every helper available everywhere.