# Quick REST question

**URL:** <https://www.ruby-forum.com/t/quick-rest-question/92882>\
**Category:** Rails\
**Created:** [April 25, 2007, 11:11am UTC](https://www.ruby-forum.com/t/quick-rest-question/92882 "2007-04-25T11:11:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [April 25, 2007, 11:11am UTC](https://www.ruby-forum.com/t/quick-rest-question/92882/1 "2007-04-25T11:11:26Z")

</div>

Is this possible?

[domain.com/users](http://domain.com/users)

[domain.com/users/1/products](http://domain.com/users/1/products)

[domain.com/products](http://domain.com/products)

I’d like to view all the products for a User and also all of the  
entire products. From what I’ve tried, this does not work:

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

map.resources :products

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [April 25, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/quick-rest-question/92882/2 "2007-04-25T12:01:33Z")

</div>

You have to use a name\_prefix for the nested resources.  
Jamis B. wrote about it two months ago:  
[http://weblog.jamisbuck.org/2007/2/5/nesting-resources](http://weblog.jamisbuck.org/2007/2/5/nesting-resources)

cheers 🙂

– bobes

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [April 25, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/quick-rest-question/92882/3 "2007-04-25T22:55:31Z")

</div>

Actually my last post didn’t work. It would read the User id as the  
action and the user\_products as the id. I changed my routes to this  
with success:

map.resources :user\_products, :path\_prefix =\> “users/:user\_id”

Is this the best way to do this?

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [April 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/quick-rest-question/92882/4 "2007-04-25T23:04:32Z")

</div>

Looks like my second post didn’t go through. Here is what I had

map.resources :users do |users|  
users.resources :products, :controller =\> “user\_products”  
end

map.resources :products

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [April 26, 2007, 12:06am UTC](https://www.ruby-forum.com/t/quick-rest-question/92882/5 "2007-04-26T00:06:36Z")

</div>

So you’ve got a ProductsController and UserProductsController? If so,  
then  
this could be a good way to go.  
If the views are the same for user products and all products than having  
one  
controller and one set of views is a better solution.  
The routes would be:

map.resources :users do |users|  
users.resources :products, :name\_prefix =\> “user\_”  
end  
map.resources :products

And in actions you would have something like:

def index  
params[:user\_id].nil? ? Products.find(:all) : User.find  
(params[:user\_id]).products  
end

You could also extract the condition to a before\_filter, let me know if  
you  
have any problems with this…

– bobes

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [April 26, 2007, 1:13am UTC](https://www.ruby-forum.com/t/quick-rest-question/92882/6 "2007-04-26T01:13:40Z")

</div>

I actually needed to use the same views so this works perfectly. I  
ended up using an if/else loop because I needed to render one  
different view.  
Thanks!  
(now I have to go back and delete all the user\_products files …)

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [April 26, 2007, 1:50am UTC](https://www.ruby-forum.com/t/quick-rest-question/92882/7 "2007-04-26T01:50:42Z")

</div>

Thanks a lot bobes! Specifically comment number 15 was exactly what I  
was looking for. My routes now look like this:

map.resources :users do |users|  
users.resources :products, :controller =\> “user\_products”  
end

map.resources :products

Now I obviously had to create an extra controller:  
UserProducts \< ApplicationController
