# Nested Resources and Routes

**URL:** <https://www.ruby-forum.com/t/nested-resources-and-routes/161304>\
**Category:** Rails\
**Created:** [February 25, 2009, 9:27am UTC](https://www.ruby-forum.com/t/nested-resources-and-routes/161304 "2009-02-25T09:27:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [February 25, 2009, 9:27am UTC](https://www.ruby-forum.com/t/nested-resources-and-routes/161304/1 "2009-02-25T09:27:25Z")

</div>

hi,

i have some nested resources like, a deal has\_many :orders and has\_one  
:invoice  
i am using nested routing for these. so the urls are like:

deals\_url  
deal\_orders\_url  
deal\_invoice\_url

and others for the whole CRUD

although everything is working perfectly, the updates for a nested  
resource  
are stuck. e.g. the edit page for an order comes up, buts submitting it  
gives me an error:

Couldn’t find Order without an ID  
app/controllers/duties\_controller.rb:92:in `get\_order’

i have created a before\_filter :get\_deal

def get\_deal  
@deal = Deal.find(params[:deal\_id])  
end

do i need to change something in my edit/update actions?? right now they  
are  
default ones generated after scaffolding.

Regards

–  
Sahil

---

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [February 26, 2009, 8:47am UTC](https://www.ruby-forum.com/t/nested-resources-and-routes/161304/2 "2009-02-26T08:47:41Z")

</div>

On Feb 25, 11:23 pm, Richard A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You need to change the way you reference orders so that you scope an order  
> within a deal.

thank for the suggestion Richard… but i managed to do it by adding  
an :except clause to the before filter to work on only new and edit

---

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [February 25, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/nested-resources-and-routes/161304/3 "2009-02-25T19:24:02Z")

</div>

You need to change the way you reference orders so that you scope an  
order  
within a deal.

Give this a shot and let me know if it works:

order\_controller.rb:

Index action:  
@order = @deal.orders.find(:all)

Show action:  
@order = @deal.orders.find(params[:id])

New action:  
@order = @deal.orders.new

Edit action:  
@order = @deal.orders.find(params[:id])

Create action:  
@order = @deal.orders.new(params[:order])

Update action:  
@order = @deal.orders.find(params[:id])

Delete action:  
@order = @deal.orders.find(params[:id])

On Wed, Feb 25, 2009 at 3:26 AM, Sahil D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> and others for the whole CRUD  
> def get\_deal
> 
> >

–  
-Richard A.
