Controller Action delegating

Case:

I have a controller that creates an object. However in the very same form, I also want to make a subobject.

That subobject could also be made standalone.

So when I click submit on the original I get the input of the object and subobject in the controller action for the main object. I’d like to delegate creation of that subobject to another controller. Is that possible, or am I doing it wrong?

Is delegating to another controller WITH return values possible in general?

This can be a quite philosophical discussion as far as I see that :)

My opinion:

If your subObject ALWAYS comes with a mainObject then I would create both instances in the mainObject’s create action, validating and saving both of them in the same controller. If however both objects don’t have to be connected then you could instantiate the mainObject first, rendering the form the classic way but add an ajax button that partially renders the subObject form via calling its controller’s create function on demand. This would also be quite clean.

What you want to do is like I said first, but delegating the creation of the subobject to the subObject controller WITHIN your mainObject controller if I understand that correctly. I don’t think that this is a good approach as you would have to put additional logic into the controller like “is this a normal GET request, a POST request, ajax request via POST or GET or did the other controller just asked me to create and save a model?”

Before sharing things between controllers, making them fat and harder to maintain you should think about adding such logic to the models because those functions could be called by both controllers without additional logic in there. In general fat models <-> skinny controllers are the way to go

I decided to go the first route, they should indeed always be related. In fact, you can’t create one without the other. I have thus also removed all crud for the subobject (and integrated it with the main object).

Thanks for the reply, it helped.