# Routing requests without ID parameter

**URL:** <https://www.ruby-forum.com/t/routing-requests-without-id-parameter/59211>\
**Category:** Rails\
**Created:** [May 1, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/routing-requests-without-id-parameter/59211 "2006-05-01T20:24:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [May 1, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/routing-requests-without-id-parameter/59211/1 "2006-05-01T20:24:43Z")

</div>

Hi there,

This seems to be obvious but after reading a few times the section in  
the Agile Wb Dev with Rails I’m still strugling.

I have a table with categories names which I want to display in the url  
instead of their id. So far I managed to get both in the url by adding:

map.connect ‘/:id/:category/’, :controller =\> “site”, :action =\> “show”

in the routes.rb file and calling the link in the view as below:

\<%= link\_to category.name, :action =\> ‘show’, :id =\> category, :category  
=\> category.name %\>

I tried to take the id out of map.connect but then I get the error  
message below as the controller is looking by id’s and not the category  
name:

“Couldn’t find Category without an ID”

Should I modify the controller to look by the name parameter instead of  
the id or am I missing something in the routes.rb file?

Anyone there who can help out?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [May 2, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/routing-requests-without-id-parameter/59211/2 "2006-05-02T23:02:27Z")

</div>

> Should I modify the controller to look by the name parameter instead of  
> the id or am I missing something in the routes.rb file?
> 
> Anyone there who can help out?
> 
> Thanks a lot.

If you want only the category in the url…

map.connect ‘:category/’, :controller =\> “site”, :action =\> “show”

Then in you’re controller use

Model.find\_by\_category

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [May 2, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/routing-requests-without-id-parameter/59211/3 "2006-05-02T23:30:40Z")

</div>

What actions besides /site/show do you want to do this on? If you want  
to show an individual item, you probably want /site/show\_item/33 to  
work, right?

Then for this one action you’d want something like

map.connect ‘/site/show/:category’, :controller =\> “site”, :action =\>  
“show”

or since you probably have a couple of actions that can work like this  
but a lot of others that need a numeric ID, maybe have one controller  
where all the actions use the :category string as the identifier, in  
which case you could say

map.connect ‘/site/:action/:category’, :controller =\> “site”

and in your show method in the controller you can’t use  
Foobar.find(params[:id]). Instead you’d need something like

Foobar.find\_by\_category(params[:category])

Diego wrote:

> Hi there,
> 
> This seems to be obvious but after reading a few times the section in  
> the Agile Wb Dev with Rails I’m still strugling.
> 
> I have a table with categories names which I want to display in the url  
> instead of their id. So far I managed to get both in the url by adding:
> 
> map.connect ‘/:id/:category/’, :controller =\> “site”, :action =\> “show”
> 
> in the routes.rb file and calling the link in the view as below:
> 
> \<%= link\_to category.name, :action =\> ‘show’, :id =\> category, :category  
> =\> category.name %\>
> 
> I tried to take the id out of map.connect but then I get the error  
> message below as the controller is looking by id’s and not the category  
> name:
> 
> “Couldn’t find Category without an ID”
> 
> Should I modify the controller to look by the name parameter instead of  
> the id or am I missing something in the routes.rb file?
> 
> Anyone there who can help out?
> 
> Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Diego](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@Diego](https://www.ruby-forum.com/u/Diego)\
**Post date:** [May 3, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/routing-requests-without-id-parameter/59211/4 "2006-05-03T18:58:09Z")

</div>

yes, your controller can only see what you’ve passed to it in the url or  
a post. try using find\_by\_name in your controller.

Charlie B.  
[www.recentrambles.com](http://www.recentrambles.com)
