# RESTful Nesting issue

**URL:** <https://www.ruby-forum.com/t/restful-nesting-issue/101510>\
**Category:** Rails\
**Created:** [August 5, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/restful-nesting-issue/101510 "2006-08-05T17:05:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 5, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/restful-nesting-issue/101510/1 "2006-08-05T17:05:37Z")

</div>

Iâ??m having a strange issue with nested routes though, and was wondering  
if anyone could shed some light on thisâ?¦

If I have:

map.resources :users do |users|  
users.resources :items  
end

When I try to call

\<%= link\_to\_remote ‘Delete’, :url =\> item\_url(@item), :with =\>  
“‘\_method=delete’”, :confirm =\> ‘Are you sure?’ %\>

From inside the user url

[http://localhost:3001/users/1/items](http://localhost:3001/users/1/items)

Or edit\_item\_url(@item.id) it doesnâ??t always render the correct URL, and  
sometimes I get a nil error trying to render the url.

Not sure what Iâ??m missing here, but any help is greatly appreciated.  
Thanks.

I’ve freezed Edge Rails btw.

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 5, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/restful-nesting-issue/101510/2 "2006-08-05T17:30:15Z")

</div>

Still getting to grips with this myself, but I _think_ you still need to  
explicitly declare the items routes as well, so:  
map.resources :items

Rick’s routing\_navigator helps with showing what routes are available  
and that’s help me some, although as I said, still getting to grips with  
it myself…

HTH

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 5, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/restful-nesting-issue/101510/3 "2006-08-05T18:40:52Z")

</div>

Guest wrote:

> Iâ??m having a strange issue with nested routes though, and was wondering  
> if anyone could shed some light on thisâ?¦
> 
> If I have:
> 
> map.resources :users do |users|  
> users.resources :items  
> end
> 
> When I try to call
> 
> \<%= link\_to\_remote ‘Delete’, :url =\> item\_url(@item), :with =\>  
> “‘\_method=delete’”, :confirm =\> ‘Are you sure?’ %\>
> 
> From inside the user url
> 
> [http://localhost:3001/users/1/items](http://localhost:3001/users/1/items)
> 
> Or edit\_item\_url(@item.id) it doesnâ??t always render the correct URL, and  
> sometimes I get a nil error trying to render the url.

When you use the x\_url or x\_path helpers you can pass either a hash of  
options or positional params. If you go with positional params, they  
have to correspond to the order of params in the route. In your example,  
the route URL would look like

/users/:user\_id/items/:id

Therefore you need to say

item\_url(@user, @item)  
or  
items\_url(@user)

That should fix it. You might also consider using item\_path instead of  
item\_url. It’s the same without the protocol, host and port bits of the  
URL, which are generally redundant for that usage.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 5, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/restful-nesting-issue/101510/4 "2006-08-05T18:51:20Z")

</div>

try

\<%= link\_to\_remote ‘Delete’, :url =\> item\_url(@item.user, @item), :with  
=\>  
“‘\_method=delete’”, :confirm =\> ‘Are you sure?’ %\>

(asuming Item has a user association)  
nesting broke my neck yesterday, too, because i didn’t think of the  
:with  
option and tried  
\<%= link\_to\_remote ‘delete’, :url =\> item\_url(@item.user, @item,  
:\_method =\>  
‘delete’) %\>  
but, as you can think that doesnt work at all

2006/8/5, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> When I try to call
> 
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 6, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/restful-nesting-issue/101510/5 "2006-08-06T19:47:19Z")

</div>

This is all pretty helpful. Thanks guys. Another thing, is there a way  
to get the user from the url?

I’m going to complicate this even more now…

Items can belong to users, and categories. So…

I have this in my routes file:

map.resources :categories do |categories|  
categories.resources :items  
end

map.resources :users do |users|  
users.resources :items  
end

Now, if I’m at users/1/items I need a way of being able to have that  
user id. However if I’m in categories/1/items I need the category ID. Do  
I have to do an explicit find for each in the index action for items  
like:

@user = User.find(params[:user\_id])

and

@categroy = Category.find(prams[:category\_id])

?

There has to be a better way than this.

Thanks again.

Michael S. wrote:

> try
> 
> \<%= link\_to\_remote ‘Delete’, :url =\> item\_url(@item.user, @item), :with  
> =\>  
> “‘\_method=delete’”, :confirm =\> ‘Are you sure?’ %\>
> 
> (asuming Item has a user association)  
> nesting broke my neck yesterday, too, because i didn’t think of the  
> :with  
> option and tried  
> \<%= link\_to\_remote ‘delete’, :url =\> item\_url(@item.user, @item,  
> :\_method =\>  
> ‘delete’) %\>  
> but, as you can think that doesnt work at all
> 
> 2006/8/5, Guest [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > When I try to call
> > 
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –  
> Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> [www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 6, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/restful-nesting-issue/101510/6 "2006-08-06T21:56:22Z")

</div>

On Aug 6, 2006, at 10:47 AM, Guest wrote:

> ```
> categories.resources :items
> 
> ```
> 
> like:
> 
> @user = User.find(params[:user\_id])
> 
> and
> 
> @categroy = Category.find(prams[:category\_id])
> 
> ?
> 
> There has to be a better way than this.

I think this is pretty nice:

before\_filter :find\_category  
before\_filter :find\_item, :except =\> [:index, :new, :create]

private  
def find\_category  
@category = Category.find(params[:category\_id])  
end

```
 def find_item
   @item = @category.items.find(params[:id])
 end

```

Is it too much code? or too repetitive? Could dry it out for  
resources which follow the /categories/1/items/2 convention -

# in a base class

before\_filter :find\_resources

# imagine resource\_chain is extracted from the route as

[[‘categories’, 1], [‘items’, 2]]

# set @category = Category.find(1), @item = @category.items.find

(2), etc.  
def find\_resources  
name, id = resource\_chain.shift  
root = name.classify.constantize.find(id)  
instance\_variable\_set “@#{name.singularize}”, root  
resource\_chain.inject(root) do |last, (associate, id)|  
instance\_variable\_set “@#{associate.singularize}”, last.send  
(associate).find(id)  
end  
end

Then your resource controllers can drop the find\_\* before\_filters.

jeremy
