Achaius
Achaius

Reputation: 6124

Calling helper method from Rails 3 controller

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

Upvotes: 29

Views: 20390

Answers (3)

Nikhil Thombare
Nikhil Thombare

Reputation: 1198

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

Upvotes: 1

Alexey
Alexey

Reputation: 9447

 view_context.some_helper_method

Upvotes: 36

clyfe
clyfe

Reputation: 23770

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

Upvotes: 30

Related Questions