Why can I access helper methods for one controller in the views for a different controller? Is there a way to disable this without hacking/patching Rails?
A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.
Basically helpers in Rails are used to extract complex logic out of the view so that you can organize your code better. I've seen two benefits for using helpers in my experience so far: Extract some complexity out of the view. Make view logic easier to test.
Custom HelpersEdit Custom helpers for your application should be located in the app/helpers directory.
In Rails 5, by using the new instance level helpers method in the controller, we can access helper methods in controllers.
@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.
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.
Delete the following line from ApplicationController
helper :all
In this way each controller will load its own helpers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With