# Routes and link generation

**URL:** <https://www.ruby-forum.com/t/routes-and-link-generation/87020>\
**Category:** Rails\
**Created:** [February 27, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020 "2007-02-27T17:54:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [February 27, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/1 "2007-02-27T17:54:32Z")

</div>

Hi,  
I’m setting up some routes for an online shop. I want to the urls to  
look like:

/category/department/product

The problem I’m having is how to generate the urls needed. Do I have to  
specify  
the category and department for each link? The problem is that a product  
can be  
in more than one category so I won’t know what to pass.

I currently have this in routes.rb:

map.with\_options :controller =\> ‘catalog’ do |catalog|  
catalog.product ‘:category/:department/:id’, :action =\> ‘show’  
end

And this as my link:

\<%= link\_to ‘A special banana’, product\_url(:category =\> ‘fruit’,  
:department =\> ‘bananas’, :id =\> 444) %\>

Do I need to override to\_param for the product? If so, how?

Thanks for any advice.

Cheers,  
Jord

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [February 28, 2007, 10:20am UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/2 "2007-02-28T10:20:43Z")

</div>

Not sure if I was clear enough. What I don’t understand is how to  
figure out which category and department a product is so that I can  
generate the links.

Am I making too much work for myself by have the routes setup like I  
want? Is it possible to have routes like this or would it be better to  
go for something like:

/product/1

I think a route like /category/department/product is a lot more  
readable and can already do this if a product only appears in one  
category, but I’m not sure how to go about it when a product is in  
more than one category.

What do you think?

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 2, 2007, 3:04pm UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/3 "2007-03-02T15:04:26Z")

</div>

If you’ve set up your route like so

map.product “/:category/:department/:product”, :controller =\> […]

then you should be able to generate the url like this

product\_url(:category =\> category, :department =\> :department). If  
you’re  
looking to avoid the numeric representations of category and department,  
then make sure to redefine to\_params in your models for each.

class Category  
[…]  
def to\_params  
name # Or whatever attribute you want…  
end  
end

Is that what you’re looking for?

RSL

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 2, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/4 "2007-03-02T20:05:08Z")

</div>

Thanks for the reply Russell, but that’s what I have already.

The problem was with generating a route to a product that exists in  
more than one department. Sya I was generating a route to a product  
from the home page, I wouldn’t know which category to put for the url.  
Anyway, I’ve decided that it’s probably more effect than it’s worth.  
So, I’ve decided to go for /product/123-product-name for my product  
urls and stick with /category/department for category display.

Thanks anyway 🙂

Cheers,  
Jord

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 3, 2007, 1:42am UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/5 "2007-03-03T01:42:57Z")

</div>

> I don’t understand why
> 
> product\_url(:category =\> category, :department =\> department)
> 
> wouldn’t assign category based solely on the fact that you manually told it  
> which category to use. I’ve never had the multiple category setup but I  
> can’t imagine that Rails wouldn’t respond to setting a param manually.

It will, but say that I have a product which appears in both category  
A and B, how will I know which category, either A or B, to pass to  
product\_url? It works ok if you are within the category when  
generating the route because you can grab the category from  
params[:category], but if you’re not in the category, for example, on  
the home page, you won’t know which category to pass to the  
product\_url because there are two categories associated with the  
product.

Does that make sense? 🙂

Cheers,  
Jord

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 2, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/6 "2007-03-02T23:18:42Z")

</div>

I don’t understand why

product\_url(:category =\> category, :department =\> department)

wouldn’t assign category based solely on the fact that you manually told  
it  
which category to use. I’ve never had the multiple category setup but I  
can’t imagine that Rails wouldn’t respond to setting a param manually.

RSL

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 3, 2007, 1:58am UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/7 "2007-03-03T01:58:57Z")

</div>

Jordan,

Jamis wrote about some of these issues recently:

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

~ j.

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 3, 2007, 10:43am UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/8 "2007-03-03T10:43:27Z")

</div>

> Jamis wrote about some of these issues recently:
> 
> [Buckblog: Nesting resources](http://weblog.jamisbuck.org/2007/2/5/nesting-resources)

Thanks, I’ll take a look 🙂

Cheers,  
Jord

---

<div class="post-metadata">

**Author:** ![jordelver](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@jordelver](https://www.ruby-forum.com/u/jordelver)\
**Post date:** [March 3, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/routes-and-link-generation/87020/9 "2007-03-03T14:47:46Z")

</div>

Yeah. That does make sense. I guess if it weren’t coming from a  
category-specific archive/page/whatever, what I’d do is pass it  
product.categories.first. I just tested [in irb] this kind of setup and  
it  
works nicely.

product\_url(:category =\> @category || product.categories.first || nil,  
:department =\> :whatever}

if @category is set, then it uses it, else the first product.category,  
and  
if product.categories.empty? then it uses nil.

RSL
