gpt4 book ai didi

ruby-on-rails - rails : How to POST internally to another controller action?

转载 作者:行者123 更新时间:2023-12-03 16:10:54 24 4
gpt4 key购买 nike

这听起来很奇怪,但请听我说……我需要能够向我的其他 Controller 之一发出等效的 POST 请求。 SimpleController基本上是一个更详细的 Controller 的简化版本。我怎样才能适本地做到这一点?

class VerboseController < ApplicationController
def create
# lots of required params
end
end

class SimpleController < ApplicationController
def create
# prepare the params required for VerboseController.create
# now call the VerboseController.create with the new params
end
end

也许我想太多了,但我不知道该怎么做。

最佳答案

Rails 应用程序(或任何遵循相同模型-适配器- View 模式的 Web 应用程序)中的 Controller 间通信是您应该积极避免的。当您想这样做时,请认为这表明您正在与构建应用程序的模式和框架作斗争,并且您所依赖的逻辑已在应用程序的错误层实现。

正如@ismaelga 在评论中建议的那样;两个 Controller 都应该调用一些通用组件来处理这种共享行为并保持你的 Controller “瘦”。在 Rails 中,这通常是模型对象上的一种方法,尤其是对于在这种情况下您似乎担心的那种创建行为。

关于ruby-on-rails - rails : How to POST internally to another controller action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10592353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com