我有以下 Controller :
class FirstController < ApplicationController
helper_method :contoller_method
private
def contoller_method
"text"
end
end
我如何使用
contoller_method
在另一个 Controller 看来?有最佳实践吗?
最佳答案
将该方法放入 application_controller.rb
.然后它将对您的所有 Controller 可用。
如果你只想在两个类(class)之间共享它,你可以做这样的事情。创建一个名为 helper Controller 的新 Controller ,并从它继承第一个/第二个 Controller 。
class FirstController < HelperController
end
class SecondController < HelperController
end
class HelperController < ApplicationController
helper_method :contoller_method
private
def contoller_method
"text"
end
end
关于ruby-on-rails - rails : Use helper_method from one controller in another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4174593/