NoMethodError when trying to invoke helper method from Rails controller

Helper Methods from Controllers

One way to get at your helper methods is simply to include your helper file.

include LoginHelper
cool_login_helper_method(x,y,z)

This brings all the methods from that helper module into scope in your controller. That's not always a good thing. To keep the scope separate, create an object, imbue it with the powers of that helper, and use it to call the methods:

login_helper = Object.new.extend(LoginHelper)
login_helper.cool_login_helper_method(x,y,z)

Helper :all

helper :all makes all of your helper methods from all of your helper modules available to all of your views, but it does nothing for your controllers. This is because helper methods are designed for use in views and generally shouldn't be accessed from controllers. In newer versions of Rails, this option is always on for every controller by default.


if you need to share a method between a controller and helper/view, you can just define via 'helper_method' in the top of the controller:

class ApplicationController < ActionController::Base
  helper_method :my_shared_method
  ...

  def my_shared_method
    #do stuff
  end
end

hope that helps


helper :all makes all the helpers (yes, all of them) available in the views, it does not include them into the controller.

If you wish to share some code between helper and controller, which is not very desirable because helper is UI code and controller is, well, controller code, you can either include the helper in the controller, or create a separate module and include that in the controller and the helper as well.


To use the helper methods already included in the template engine:

  • Rails 2: use the @template variable.
  • Rails 3: has the nice controller method view_context

Example usage of calling 'number_to_currency' in a controller method:

# rails 3 sample
def controller_action
  @price = view_context.number_to_currency( 42.0 ) 
end

# rails 2 sample
def controller_action
  @price = @template.number_to_currency( 42.0 ) 
end