Reputation: 10162
I have the following controller:
class FirstController < ApplicationController
helper_method :contoller_method
private
def contoller_method
"text"
end
end
How can I use contoller_method
in the view of another controller? Is there a best practice?
Upvotes: 3
Views: 3951
Reputation: 9825
Maybe this?
class FirstController
include SomeConcern
end
class SecondController
include SomeConcern
end
module SomeConcern
def self.included(base)
base.helper_method :controller_method
end
private
def controller_method
"text"
end
end
Upvotes: 4
Reputation: 21497
Place the method in the application_controller.rb
. Then it'll be available to all your controllers.
If you only wanted to share it between two classes you could do something like this. Create a new controller called helper controller and have the First/Second controller inherit from it.
class FirstController < HelperController
end
class SecondController < HelperController
end
class HelperController < ApplicationController
helper_method :contoller_method
private
def contoller_method
"text"
end
end
Upvotes: 9