# Nested Resources. What does the controller look like?

**URL:** <https://www.ruby-forum.com/t/nested-resources-what-does-the-controller-look-like/106781>\
**Category:** Rails\
**Created:** [June 14, 2007, 1:54am UTC](https://www.ruby-forum.com/t/nested-resources-what-does-the-controller-look-like/106781 "2007-06-14T01:54:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [June 14, 2007, 1:54am UTC](https://www.ruby-forum.com/t/nested-resources-what-does-the-controller-look-like/106781/1 "2007-06-14T01:54:59Z")

</div>

Hi,

A recent thread on the ruby list has got me thinking about what a  
controller  
would look like for nested resources.

> However, in terms of template and controller lines, Damage probably  
> (simplistically) only accounts for about 20% the application total.

What would the damages controller look like in this situation? How much,  
and  
what code goes into determining what objects are to be setup?

For my project I have a situation where I have

map.resources :books, :has\_many =\> :clips  
/books/1/clips

This is fine, I can deal with this, but I would also like another part  
where  
I can look into others books

/public/username/books/1/clips

But I don’t really know what the controller should look like to set this  
up.

Any tips or links to discussion on this would be great

Cheers  
Daniel

[http://www.techsmith.com/featured/snag](http://www.techsmith.com/featured/snag)

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [June 14, 2007, 2:51am UTC](https://www.ruby-forum.com/t/nested-resources-what-does-the-controller-look-like/106781/2 "2007-06-14T02:51:05Z")

</div>

On Jun 13, 2007, at 7:53 PM, Daniel N wrote:

> house.resources :damages  
> What would the damages controller look like in this situation? How
> 
> /public/username/books/1/clips
> 
> But I don’t really know what the controller should look like to set  
> this up.
> 
> Any tips or links to discussion on this would be great
> 
> Cheers  
> Daniel

Not that I know where the :has\_many option on resources is supposed  
to go. (this would be on your MODEL not the route)

Anyway, you can use the #with\_options method to automagically add  
options to all method calls in a block:

map.with\_options :controller =\> :libraries do |library|  
library.borrow ‘borrow/:id’, :action =\> ‘borrow’  
library.request ‘request/:id’, :action =\> ‘request’  
end

is the same as:

map.borrow ‘borrow/:id’, :controller =\> :libraries, :action =\> ‘borrow’  
map.request ‘request/:id’, :controller =\> :libraries, :action =\>  
‘request’

Look at:  
[http://api.rubyonrails.org/classes/ActionController/Routing.html](http://api.rubyonrails.org/classes/ActionController/Routing.html)  
[http://api.rubyonrails.org/classes/ActionController/Resources.html](http://api.rubyonrails.org/classes/ActionController/Resources.html)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [June 14, 2007, 2:59am UTC](https://www.ruby-forum.com/t/nested-resources-what-does-the-controller-look-like/106781/3 "2007-06-14T02:59:06Z")

</div>

On 6/14/07, Josh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In edge rails you can now call has\_many and has\_one to include nested  
> resources, thus the example is valid.
> 
> –  
> Josh K.  
> removed\_email\_address@domain.invalid  
> [http://joshknowles.com](http://joshknowles.com)

To tell you the truth, the routing is only a secondary issue for the  
question at hand.

What I’m really after is to know what the damages controller would look  
like. How does it manage the owning resource is that was called?

Cheers

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [June 14, 2007, 2:56am UTC](https://www.ruby-forum.com/t/nested-resources-what-does-the-controller-look-like/106781/4 "2007-06-14T02:56:06Z")

</div>

On 6/13/07, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Not that I know where the :has\_many option _on resources_ is supposed to  
> go. (this would be on your MODEL not the route)

In edge rails you can now call has\_many and has\_one to include nested  
resources, thus the example is valid.

–  
Josh K.  
removed\_email\_address@domain.invalid  
[http://joshknowles.com](http://joshknowles.com)
