Where to put Ruby helper methods for Rails controllers?
Asked Answered
N

6

81

I have some Ruby methods certain (or all) controllers need. I tried putting them in /app/helpers/application_helper.rb. I've used that for methods to be used in views. But controllers don't see those methods. Is there another place I should put them or do I need to access those helper methods differently?

Using latest stable Rails.

Neogaea answered 28/11, 2012 at 19:48 Comment(0)
H
79

You should define the method inside ApplicationController.

Harrelson answered 28/11, 2012 at 19:54 Comment(3)
He will also have to add helper_method :my_helper_method in ApplicationController to make them available to the views.Intercommunion
wont this lead to a fat controller over time?Rigger
also @Intercommunion that violates MVCRigger
T
76

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
  end
end

Then, in the controller just include it

class BadgeController < ApplicationController
  include EventsHelper

  ...
end
Trolley answered 9/10, 2014 at 13:3 Comment(5)
I find this the nicest solution when you don't need the helper methods included in all your controllers. This solution also applies to models.Totality
Do you know if we need to use extend ActiveSupport::Concern with controller concerns?Drogue
No need to do extend ActiveSupport::Concern inside controller/concerns directory. You just need to define modules above directory and can simply do include as mentioned above(for rails 4.2 and rails 5).Overglaze
probably the best answerPanicle
Can this method be accessed from View?Samiel
A
30

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
  end
end

You can also include helper files as follow

class YourController < ApplicationController
  include OneHelper
  include TwoHelper
end
Ain answered 28/11, 2012 at 20:2 Comment(2)
is there a way to assign all the helper_methods on one line?Wastepaper
@HarshaMV I think you can just go ahead like helper_method :first_method, :second_method.Rheinland
I
21

You can call any helper methods from a controller using the view_context, e.g.

view_context.my_helper_method
Intercommunion answered 28/11, 2012 at 20:0 Comment(1)
There's a more modern version of this answer for Rails 5+: https://mcmap.net/q/116632/-rails-how-to-use-a-helper-inside-a-controllerUnderling
D
8

Ryan Bigg response is good.

Other possible solution is add helpers to your controller:

class YourController < ApplicationController
  include OneHelper
  include TwoHelper
 end

Best Regards!

Decapitate answered 28/11, 2012 at 19:59 Comment(0)
U
1

Including helpers in controller will end-up exposing helper methods as actions!

# With new rails (>= 5) 

helpers.my_helper_method


# For console

helper.my_helper_method
Unscratched answered 8/1, 2020 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.