Do you have to manually include ApplicationHelper in ApplicationController in Rails 6?

Viewed 3225

Controller:

class FooController < ApplicationController
  def index
    bar_method

ApplicationHelper:

module ApplicationHelper
  def bar_method

Viewing the index page raises undefined method bar_method.

Does ApplicationHelper need to be manually included?

Rails 6.

2 Answers

You don't need to include helper module. With helpers (in rails 5+), you can access the helper methods. Try this:

class FooController < ApplicationController
  def index
    helpers.bar_method

It turns out this was an update to Rails before 6.

In previous versions of Rails the controller will include a helper which matches the name of the controller, e.g., MyController will automatically include MyHelper. To return old behavior set config.action_controller.include_all_helpers to false.

https://api.rubyonrails.org/classes/ActionController/Helpers.html

Related