How to define helper method in concerns of controllers

4.4k Views Asked by At

I have a ColumnSort module in my rails project.

module ColumnSort
  extend ActiveSupport::Concern

  def sort_column
    # do something
  end
end

And I'm using it from CompaniesController and UsersController.

class CompaniesController < ApplicationController
  include ColumnSort
  helper_method :sort_column
end

class UsersController < ApplicationController
  include ColumnSort
  helper_method :sort_column
end

It works fine. But I want to write the line helper_method :sort_column in the module ColumnSort. How can I write it?

1

There are 1 best solutions below

3
On

You don't have to call the helper_method. helper_method is to expose a controller method to the view not to import methods from a helper class.

include ColumnSort

This line, includes already all methods contained within the Helper Class.

You can read more about it in the Rails API docs: http://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method