# REST routes & link\_to paths

**URL:** <https://www.ruby-forum.com/t/rest-routes-link-to-paths/133217>\
**Category:** Rails\
**Created:** [March 28, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/rest-routes-link-to-paths/133217 "2008-03-28T22:23:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@Sav](https://www.ruby-forum.com/u/Sav)\
**Post date:** [March 28, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/rest-routes-link-to-paths/133217/1 "2008-03-28T22:23:50Z")

</div>

I’ve mapped this nested route for a product catalog…

map.resources :brands do |brand|  
brand.resources :product\_lines do |product\_line|  
product\_line.resources :products do |product|  
product.resources :upcs  
end  
end  
end

And the pretty URLS are working great (when manually typed)…  
/brands/1/product\_lines/5/products/10/upcs/15/edit

My problem is I don’t know how to construct those pretty links in my  
link\_to and redirect\_to paths…

product\_lines\_path(:brand\_id =\> params[:id])

# returns: /product\_lines?brand\_id=4

# desired: /brands/4/product\_lines

link\_to product\_path(@product)

# returns: /products/4

# desired: /brands/1/product\_lines/5/products/4

Any ideas? Thanks.

---

<div class="post-metadata">

**Author:** ![Sav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@Sav](https://www.ruby-forum.com/u/Sav)\
**Post date:** [March 28, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/rest-routes-link-to-paths/133217/2 "2008-03-28T22:53:19Z")

</div>

Sav wrote:

> My problem is I don’t know how to construct those pretty links in my  
> link\_to and redirect\_to paths…

Whenever you are looking into a routing issue, your first step should  
be:

rake routes

run in your application top level and understand what this is telling  
you.

> product\_lines\_path(:brand\_id =\> params[:id])
> 
> # returns: /product\_lines?brand\_id=4
> 
> # desired: /brands/4/product\_lines

brand\_product\_lines\_path 1  
or:  
brand\_product\_lines\_path :brand\_id =\> 1

> link\_to product\_path(@product)
> 
> # returns: /products/4
> 
> # desired: /brands/1/product\_lines/5/products/4

brand\_product\_line\_product\_path 1, 5, 4  
or:  
brand\_product\_line\_product\_path :brand\_id =\> 1,  
:product\_line\_id =\> 5, :product\_id =\> 4

These URLs are pretty cumbersome. Have a look here for some advise on  
nested resources:

[http://weblog.jamisbuck.org/2007/2/5/nesting-resources](http://weblog.jamisbuck.org/2007/2/5/nesting-resources)
