简体   繁体   English

Rails:如何在内部POST到另一个控制器动作?

[英]Rails: How to POST internally to another controller action?

This is going to sound strange, but hear me out...I need to be able to make the equivalent of a POST request to one of my other controllers. 这听起来很奇怪,但是听我说......我需要能够向我的其他控制器发出相应的POST请求。 The SimpleController is basically a simplified version of a more verbose controller. SimpleController基本上是更详细的控制器的简化版本。 How can I do this appropriately? 我怎么能这样做呢?

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

Maybe I am over-thinking this, but I don't know how to do this. 也许我过度思考这个,但我不知道该怎么做。

Inter-controller communication in a Rails app (or any web app following the same model-adapter-view pattern for that matter) is something you should actively avoid. Rails应用程序(或任何遵循相同模型 - 适配器 - 视图模式的Web应用程序)中的控制器间通信是您应该主动避免的。 When you are tempted to do so consider it a sign that you are fighting the patterns and framework your app is built on and that you are relying on logic has been implemented at the wrong layer of your application. 当你想要这样做时,请考虑它是一个标志,你正在与你的应用程序构建的模式和框架作斗争,并且你依赖于逻辑已经在你的应用程序的错误层实现。

As @ismaelga suggested in a comment; 正如@ismaelga在评论中所说的那样; both controllers should invoke some common component to handle this shared behavior and keep your controllers "skinny". 两个控制器都应该调用一些通用组件来处理这种共享行为并保持控制器“瘦”。 In Rails that's often a method on a model object, especially for the sort of creation behavior you seem to be worried about in this case. 在Rails中,它通常是模型对象上的一种方法,特别是对于在这种情况下您似乎担心的创建行为。

You shouldn't be doing this. 你不应该这样做。 Are you creating a model? 你在创建一个模型吗? Then having two class methods on the model would be much better. 然后在模型上有两个类方法会好得多。 It also separates the code much better. 它还可以更好地分离代码。 Then you can use the methods not only in controllers but also background jobs (etc.) in the future. 然后,您不仅可以在控制器中使用这些方法,还可以在将来使用后台作业(等)。

For example if you're creating a Person: 例如,如果您正在创建一个Person:

class VerboseController < ApplicationController
  def create
    Person.verbose_create(params)
  end
end

class SimpleController < ApplicationController
  def create
    Person.simple_create(params)
  end
end

Then in the Person-model you could go like this: 然后在Person模型中你可以这样:

class Person
  def self.verbose_create(options)
    # ... do the creating stuff here
  end

  def self.simple_create(options)
    # Prepare the options as you were trying to do in the controller...
    prepared_options = options.merge(some: "option")
    # ... and pass them to the verbose_create method
    verbose_create(prepared_options)
  end
end

I hope this can help a little. 我希望这可以帮助一点。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:如何将表单发布到另一个控制器操作 - Rails: How to make a form post to another controller action 如何将JavaScript数据发布到Rails控制器动作? - How to post javascript data to a rails controller action? 如何显示一个控制器在导轨中的另一个控制器动作中显示动作 - how to display one controller show action in another controller action in rails 在Rails中执行获取/发布操作的最佳方法是什么? 资源如何内部转换? - What is the best way to a get/post action in Rails ? How is resources internally converted? Rails:如何在应用程序控制器中排除对before_action的发布请求 - Rails: how to exclude post requests for a before_action in the application controller 将一个控制器动作映射到另一个动作导轨上 - Map one controller action on another action rails Rails:从控制器调用另一个控制器动作 - Rails: call another controller action from a controller 如何将动作转发到Rails中的另一个控制器? - How do I forward an action to another controller in Rails? 如何创建另一个控制器动作以在Rails中创建对象? - How do I create another controller action to create an object in rails? Rails:如何在同一个控制器中将值从一个动作传递到另一个动作 - Rails: How to pass value from one action to another in the same controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM