Where to put Ruby helper methods for Rails controllers? Where to put Ruby helper methods for Rails controllers? ruby-on-rails ruby-on-rails

Where to put Ruby helper methods for Rails controllers?


For Rails 4 onwards, concerns are the way to go. There was a decent article which can still be viewed via the Wayback Machine.

In essence, if you look in your controllers folder you should see a concerns sub-folder. Create a module in there along these lines

module EventsHelper  def do_something  endend

Then, in the controller just include it

class BadgeController < ApplicationController  include EventsHelper  ...end


you should define methods inside application controller, if you have few methods then you can do as follow

class ApplicationController < ActionController::Base      helper_method :first_method  helper_method :second_method  def first_method    ... #your code  end  def second_method    ... #your code  endend

You can also include helper files as follow

class YourController < ApplicationController  include OneHelper  include TwoHelperend