Why are all Rails helpers available to all views, all the time? Is there a way to disable this? Why are all Rails helpers available to all views, all the time? Is there a way to disable this? ruby ruby

Why are all Rails helpers available to all views, all the time? Is there a way to disable this?


@George Schreiber's method doesn't work as of Rails 3.1; the code has changed significantly.

However, there's now an even better way to disable this feature in Rails 3.1 (and hopefully later). In your config/application.rb, add this line:

config.action_controller.include_all_helpers = false

This will prevent ApplicationController from loading all of the helpers.

(For anyone who is interested, here's the pull request where the feature was created.)


The answer depends on the Rails version.

Rails >= 3.1

Change the include_all_helpers config to false in any environment where you want to apply the configuration. If you want the config to apply to all environments, change it in application.rb.

config.action_controller.include_all_helpers = false

When false, it will skip the inclusion.

Rails < 3.1

Delete the following line from ApplicationController

helper :all

In this way each controller will load its own helpers.


In Rails 3, actioncontroller/base.rb (around line 224):

def self.inherited(klass)  super  klass.helper :all if klass.superclass == ActionController::Baseend

So yes, if you derive your class from ActionController::Base, all helpers will be included.

To come around this, call clear_helpers (AbstractClass::Helpers; included in ActionController::Base) at the beginning of your controller's code. Source code comment for clear_helpers:

# Clears up all existing helpers in this class, only keeping the helper# with the same name as this class.

E.g.:

class ApplicationController < ActionController::Base  clear_helpers  ...end