# Routing - newbie question about shorting the url

**URL:** <https://www.ruby-forum.com/t/routing-newbie-question-about-shorting-the-url/188518>\
**Category:** Rails\
**Created:** [May 13, 2010, 8:12am UTC](https://www.ruby-forum.com/t/routing-newbie-question-about-shorting-the-url/188518 "2010-05-13T08:12:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 13, 2010, 8:12am UTC](https://www.ruby-forum.com/t/routing-newbie-question-about-shorting-the-url/188518/1 "2010-05-13T08:12:28Z")

</div>

Hi  
Right now I have my url showing as the following:  
[http://localhost/categories/italian/recipes/special-pasta-sauce](http://localhost/categories/italian/recipes/special-pasta-sauce)

I would like it to be:  
[http://localhost/categories/italian/special-pasta-sauce](http://localhost/categories/italian/special-pasta-sauce)  
(or even [http://localhost/italian/special-pasta-sauce](http://localhost/italian/special-pasta-sauce))

my category model has\_many :recipes and my recipes model  
belongs\_to :category (for the purposes of my test project a recipe  
can only belong to one category)

Is this possible in RoR? Any pointers?

---

<div class="post-metadata">

**Author:** ![sso](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sso](https://www.ruby-forum.com/u/sso)\
**Post date:** [May 13, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/routing-newbie-question-about-shorting-the-url/188518/2 "2010-05-13T13:44:14Z")

</div>

This is possible but you will basically be setting up a rule like this

“whenever we get a url like /foo/bar then we want the category ‘foo’ and  
the recipe ‘bar’”

That might clash with the existing url schema on your site. If you want  
to go for it though then set up your routes like this:

map.category\_recipe ‘:category\_name/:recipe\_name’, :controller =\>  
‘recipes’, :action =\> ‘show’, :conditions =\> {:method =\> :get}

Make sure you put it down the bottom, above the

map.connect ‘:controller/:action/:id’

section.

Now the url [http://localhost/italian/special-pasta-sauce](http://localhost/italian/special-pasta-sauce)

should call the recipes controller, with params like {:recipe\_name =\>  
“special-pasta-sauce”, :category\_name =\> “italian”}

And in your views you can say eg  
category\_recipe\_path(category, recipe)

The above assumes that you’ve overriden to\_param in Category and Recipe  
to use the name of the object instead of it’s id.

If you’re using slus or something instead of name then you can  
substitute appropriately.
