# Display category name in url

**URL:** <https://www.ruby-forum.com/t/display-category-name-in-url/170588>\
**Category:** Rails\
**Created:** [June 19, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/display-category-name-in-url/170588 "2009-06-19T12:48:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/display-category-name-in-url/170588/1 "2009-06-19T12:48:48Z")

</div>

Hi , do we have to use controller name in url , can we change this?  
For example i have a controller named =\> category  
when user click to link “Computer Components”  
can rails write [http://domain/computer-components](http://domain/computer-components) instead of  
[http://domain/categories/53](http://domain/categories/53)

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 1:00pm UTC](https://www.ruby-forum.com/t/display-category-name-in-url/170588/2 "2009-06-19T13:00:18Z")

</div>

map.computer\_components ‘computer\_components’,:controller =\>  
“categories”,:action =\> "your\_action\_name’

Now helper will be computer\_components\_url

Sijo

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 1:45pm UTC](https://www.ruby-forum.com/t/display-category-name-in-url/170588/3 "2009-06-19T13:45:22Z")

</div>

On 19 June, 11:48, lecielbleu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi , do we have to use controller name in url , can we change this?  
> For example i have a controller named =\> category  
> when user click to link “Computer Components”  
> can rails writehttp://domain/computer-componentsinstead ofhttp://domain/categories/53

Hi,

This is possible by creating a custom route in routes.rb. You would  
need to do the following:

map.connect ‘/:category\_name’, :controller =\> ‘categories’, :action =\>  
‘show’

However, you need to be careful where this sits in your routes.rb as  
it will catch every URL if it is too high in the file. In your  
categories controller, the show action would need the following:

@category = Category.find\_by\_name(params[:category\_name]

I should point out that this is not good practice and if you intend to  
have a significant number of categories, the id should be used. The  
following link shows how you can get ‘friendly’ urls whilst still  
using standard Rails routing.

> **[Best Online Casino in Australia | Top Licensed Casinos for Gamblers](https://toponlinecasinoaustralia.com)**
>
> Best Online Casino in Australia with licensed games only. Play blackjack, roulette, poker and other top-notch casino games. Enjoy gambling 24/7.

Regards

Robin

---

<div class="post-metadata">

**Author:** ![lecielbleu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b487fb/32.png) [@lecielbleu](https://www.ruby-forum.com/u/lecielbleu)\
**Post date:** [June 19, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/display-category-name-in-url/170588/4 "2009-06-19T20:29:06Z")

</div>

Many thanks Robin , yes it’s possible doing this as you said but i  
have too much categories and subcategories (using acts\_as\_tree) ,  
Method i will use must be dynamic , if not i cant handle with  
100categories
