Using custom routes to handle new nested resources

I have a couple of resources, one nested in the other, such that my
resource spec in routes.rb is:

map.resources :parents, :has_many => :children

This will generate a route for a new child that assumes that the parent
has already been saved, something like:


But what if I want a route that handles the case where the parent
resource is also new, and thus does not have an id? One that would
generate a URL that would look like:


to handle cases where I am composing a new parent by creating many
children so that I can save everything at once?

Has anyone done this? Seems like I could do it two ways:

  1. Write my own completely custom route
  2. Explicitly write the route for /parents/:parent_id/children/new and
    just set the default value for :parent_id to “new” and make it go to the
    same place?

This is definitely an outgrowth of my resistance against saving stuff to
the database before I absolutely have to, but I thought I would float it
and see what people thought?


ordinarily you’d just do /parents/new and pass along a child attribute
values in params[ :child => {…} ]

On Jul 11, 12:40 pm, Wes G. [email protected]