29

Is it possible to call helper methods from controller? If yes how to do this in Rails 3?

Achaius
  • 5,904
  • 21
  • 65
  • 122
  • 6
    Yes it is possible. You can find answer here: http://stackoverflow.com/questions/453762/nomethoderror-when-trying-to-invoke-helper-method-from-rails-controller – alexkv Jan 18 '12 at 11:05
  • Please mark " view_context.some_helper_method" as answer thank you. – FastSolutions May 29 '14 at 19:21

3 Answers3

36
 view_context.some_helper_method
Alexey
  • 9,197
  • 5
  • 64
  • 76
30

You can either include the helper module in the controller, or define the helper as a controller method and mark it as a helper via helper_method :method_name.

class FooHelper
  def bar ... end
end

class QuxsController
  include FooHelper
end

or

class QuxsController
  private
  def bar ... end
  helper_method :bar
end
clyfe
  • 23,695
  • 8
  • 85
  • 109
1

This is working if some one wants to use ApplicationHelper method in other controllers or view just add this include ApplicationHelper give below because all your controller derived from ApplicationController.

class ApplicationController < ActionController::Base
  protect_from_forgery       
  include ApplicationHelper  
end
Nikhil Thombare
  • 1,058
  • 2
  • 11
  • 26