"undefined method" when calling helper method from controller in Rails "undefined method" when calling helper method from controller in Rails ruby-on-rails ruby-on-rails

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


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.


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:


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.