2
votes

I'm new to rails and I have this small problem about helpers and controllers. Is it possible to include my custom helper to a controller?

Let's say I have this helper class.

Module UsersHelper
  def my_helper
    ...
  end
end

Then I have this controller.

class UsersController < ApplicationController

end

Can I use the my_helper in my controller?

2

2 Answers

1
votes

Yes. you can do by using include, but I don't recommend you doing this. Because Rails constructs under MVC architecture, you can learn more about MVC before include UserHelper in your controllers.

MVC Reference:

http://guides.rubyonrails.org/getting_started.html#the-mvc-architecture
Model–view–controller

1
votes

Yes - There are several ways, not least including (mixing-in) the helper into the controller, or using the loophole explained here

But... if it's hard to do, or results in ugly code, it may not be the right way to do it. Can the method that's in the helper not be moved into the controller, and then delegated back to the helper using "helper_method"?