"undefined method" when calling helper method from controller in Rails

view_context is your friend, http://apidock.com/rails/AbstractController/Rendering/view_context

if you wanna share methods between controller and view you have further options:

  • use view_context
  • define it in the controller and make available in view by the helper_method class method http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method
  • define it in a shared module and include/extend

You cannot call helpers from controllers. Your best bet is to create the method in ApplicationController if it needs to be used in multiple controllers.

EDIT: to be clear, I think a lot of the confusion (correct me if I'm wrong) stems from the helper :all call. helper :all really just includes all of your helpers for use under any controller on the view side. In much earlier versions of Rails, the namespacing of the helpers determined which controllers' views could use the helpers.

I hope this helps.


Include ApplicationHelper in application_controller.rb file like this:

class ApplicationController < ActionController::Base
  protect_from_forgery       
  include ApplicationHelper  
end

This way all the methods defined in application_helper.rb file will be available in the controller.

You can also include individual helpers in individual controllers.