我有一个名为Children
和Father
和Mother
控制器,我需要从ChildrenController
调用FatherController
和MotherController
的方法。调用另一个控制器的方法
我需要(不是在同一请求)从ChildrenController
set_details
方法get_details
方法的JSON数据传递到两个控制器。我打算根据某些条件调用任何控制器方法。
在两个控制器中都有没有路径对于get_details
方法。 我不需要任何助手方法来编写。
我需要调用多个Controller的方法,而不是继承。
父亲控制器
class FatherController < ApplicationController
def get_details(data)
##
## I need to do some operation with the 'data' received from children controller.
end
end
母亲控制器
class MotherController < ApplicationController
def get_details(data)
##
## I need to do some operation with the 'data' received from children controller.
end
end
儿童控制器
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
请帮忙如何做到这一点还是建议我,如果有任何其他的方式来做到这一点。
谢谢。
控制器层很可能不是你想要这个逻辑去住。您可能想考虑将其推向模型/业务逻辑层,而不是尝试将数据从控制器传递到控制器。例如,创建一个普通的Ruby对象,该对象知道如何处理逻辑并将返回所需的数据。 'details = DomainObject.new(data).process'在这个DomainObject中,你可以做任何你需要的东西来提取你想要的数据。 –
我同意@CarlosRamirezIII,这可能属于模型。但是如果你真的想在控制器中使用它,你可以尝试使用一种常用方法来关注“关注”,并将其包含在每个需要该方法的控制器中。关于关注的更多信息可以在这里找到:http://stackoverflow.com/questions/14541823/how-to-use-concerns-in-rails-4 – Dan
@丹我同意你。谢谢你的评论。 –