调用另一个控制器的方法 [英] Calling a method of another controller
问题描述
我有一个名为 Children
和 Father
和 Mother
我需要从 ChildrenController $ c中调用
FatherController
和 MotherController
的方法$ c>。
I have controllers named Children
and Father
and Mother
where I need to call a method of FatherController
and MotherController
from ChildrenController
.
我需要将JSON数据传递到两个控制器( 不是在同一请求下 ) ChildrenController
的 set_details
方法的 get_details
方法。我将根据某些条件调用任何控制器方法。
I need to pass a JSON data to the both controller(not at the same request) get_details
method from set_details
method of ChildrenController
. I am going to call any of the controllers method based on some conditions.
对于 get_details <在两个控制器中都使用/ code>方法。
我不需要编写任何辅助方法。
There is no route for get_details
method in both controllers.
I don't need any helper methods to write.
我需要调用多个Controller方法,而不是通过Inheriting。
I need to call more than one Controller's method, not by Inheriting.
class FatherController < ApplicationController
def get_details(data)
##
## I need to do some operation with the 'data' received from children controller.
end
end
主控制器
Mother controller
class MotherController < ApplicationController
def get_details(data)
##
## I need to do some operation with the 'data' received from children controller.
end
end
儿童控制人
Children controller
class ChildrenController < ApplicationController
data = {
"id":"2",
"expiry_date":"25-09-2016"
}.as_json
def set_details
## get_details(data) -> FatherController
## get_details(data) -> MotherController
end
end
请帮助执行此操作或建议我是否还有其他方法。
Please help how to do this or suggest me if there is any other way to do this.
谢谢。
推荐答案
简单。使方法 .self
class MotherController < ApplicationController
def self.get_details(data)
end
end
然后:
class ChildrenController < ApplicationController
def set_details
MotherController.get_details(data)
end
end
这篇关于调用另一个控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!