-1

I have two controllers: controller A and controller B and I'm calling a controller A's actions from Controller B like this:

AController.dispatch(:get, request, response)

my question is - is it a better practice to pass on a copy of the request response objects?

What is the correct way of using ActionController::dispatch method

Clarification I have to call controller B from controller A its a constraint... now the question was specifically regarding 'dispatch'...

nadavgam
  • 2,014
  • 5
  • 20
  • 48

1 Answers1

-2

You shouldn't be using it at all in application code.

ActionController.dispatch is the internal implementation of how Rails calls your controller as middleware and how the depreachiated controller tests were implemented. Rails doesn't actually clearly destinguish between the components that are a part of the makeup framework and its "public APIs" that are intended for use in your application but this definitely the former.

The main reasons you should not be using it in this situation is that it breaks the entire idea of what controller should be doing - responding to HTTP requests when they are hit by the router.

It creates a strong coupling between different controllers (that should not be coupled) and can have very unexpected side effects.

If what you're looking to do is reuse code there are also much better options:

  • Inheritance
  • Mixins (aka Concerns)
  • Service objects
max
  • 96,212
  • 14
  • 104
  • 165
  • no answering my question whatsoever... – nadavgam Jan 28 '22 at 11:29
  • @nadavgam it does actually answer the question as you should be challenging your assumptions about how to complete whatever task it is that you're really trying to solve. That "constraint" just sounds like a bad construct. – max Jan 28 '22 at 14:11