7

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. The SimpleController is basically a simplified version of a more verbose controller. 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.

Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 1
    are you sure you need that? Wouldn't be more appropriate to have verbose create as a Model method? – Ismael Abreu May 14 '12 at 23:34
  • 6
    It's great everyone says it shouldn't be done, but sometimes it needs to be done. For example, to restore POST data after authenticating, etc. – docwhat Aug 07 '12 at 18:19
  • @TheDoctorWhat Restoring POST data after authentication is probably the *only* case where something like this would make sense, and even there, I'd look for a different approach. It certainly doesn't make sense in the context in which the question was asked. – Marnen Laibow-Koser Dec 11 '13 at 03:49
  • 1
    I totally get that this is something that you shouldn't do, and that doing it will result in dogs and cats living together, mass hysteria. But if I wanted to sing the song that ends the world, how would I **do** it? I accept my voided warranty. – Potch May 17 '12 at 07:14

2 Answers2

7

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. 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; 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.

Jonah
  • 17,918
  • 1
  • 43
  • 70
3

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:

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:

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. :-)

agronemann
  • 687
  • 4
  • 16