# Using custom routes to handle new nested resources

**URL:** <https://www.ruby-forum.com/t/using-custom-routes-to-handle-new-nested-resources/142808>\
**Category:** Rails\
**Created:** [July 11, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/using-custom-routes-to-handle-new-nested-resources/142808 "2008-07-11T18:40:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/using-custom-routes-to-handle-new-nested-resources/142808/1 "2008-07-11T18:40:24Z")

</div>

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:

/parents/:parent\_id/children/new

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:

/parents/new/children/new

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?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/using-custom-routes-to-handle-new-nested-resources/142808/2 "2008-07-11T22:51:12Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
