[英]How to call a method of a controller in a method of another controller
I want to call a method of one controller in another controller's method, and also pass parameters like below: 我想在另一个控制器的方法中调用一个控制器的方法,并且还要传递如下参数:
Controller A: 控制器A:
@var
def methodA
update(@var)
end
Controller B: 控制器B:
def update(var)
var1 = var
end
Is there any way to do this? 有什么办法吗?
Why not define the shared method in ApplicationController
instead and call it in both controllers as they each inherit it's methods. 为什么不改在
ApplicationController
定义共享方法,并在两个控制器中都继承它的方法时在两个控制器中调用它。 Like so: 像这样:
ApplicationController: ApplicationController中:
class ApplicationController < ActionController::Base
protected
def update(var)
var1 = var
end
end
Other Controllers: 其他控制器:
class SomeController < ApplicationController
def some_method()
@var = 'something'
update(@var)
end
end
class SomeOtherController < ApplicationController
def some_method()
@var = 'something'
update(@var)
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.