Keeping DRY while creating objects with nested REST models

I have rescently jumped on the REST-wagon and am enjoying it a lot.
Now I have run into a problem:

Lets say I have a model called Collection which has_many Items. I know
(from Peep Code railscast, great stuff btw) how to create new Items
belonging to a certain Collection. In other words I can create Items
for an existing Collection. Now I want to do this:

On the “create new collection” form I want to be able to create new
Items without having to create the Collection first. This means the
create action of collections_controller needs to create new Item
objects. This can be done within the create method but this will
violate the DRY principle. I would like to use the create action in
the items_controller to create the new Items.

Grateful on any input on how to do this in an elegant way.

Kindest regards

Erik L.

What is to stop you from just using the items_controller to create the
items?

Maybe we have a similar issue, although I’m not quite sure:

Lets say I have a RESTful resource called ‘Messages’.

Each Message has associated Categories. They are also CRUDable on
their own, so I have another RESTful resource called ‘Categories’

In my Message Controller I have a Create method, this obviously
instantiates @message and @categories objects. The @categories object
is essentially the same as is created by the Create method of the
Categories controller.

Is there any way to DRY this? Is it possible to call the
Categories#create method from the Message controller without having it
render the Categories form? If i have render :nothing in the
Categories#create method then I cannot create a stand alone category
(which is necessary).

Apologies if my terminology is a bit awry…

monkeyten

Your situation seems very similar to mine. Actually I found a great
answer to this in the #73-75 screencasts at http://railscasts.com/.
Have a look, it helped me a lot.

Thanks for the input.

Regards

Erik