# Restful routes

**URL:** <https://www.ruby-forum.com/t/restful-routes/147038>\
**Category:** Rails\
**Created:** [August 27, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/restful-routes/147038 "2008-08-27T21:40:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [August 27, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/restful-routes/147038/1 "2008-08-27T21:40:50Z")

</div>

Hi, I’m pretty new to the concept of restful routing and am struggling  
to find any rails 2 info that helps with what I’m looking for.

I have a regular scaffold setup and the routes are setup as default

map.resources :pages

I would like to be able to use titles in the url rather than relying on  
the id.  
Eg  
localhost:3000/pages/sometitle

instead of just

localhost:3000/pages/1

Can anyone help me out?

Thanks

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [August 27, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/restful-routes/147038/2 "2008-08-27T21:49:54Z")

</div>

In your model you can override the to\_param method like so

def to\_param  
“#{self.id}-#{self.sometitle.downcase.gsub(/[^a-z0-9]+/i, ‘-’)}”  
end

and you’ll get a route like  
localhost:3000/pages/1-sometitle

replacing sometitle with an attribute of your model.

John

On Aug 27, 2:40 pm, Dan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [August 27, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/restful-routes/147038/3 "2008-08-27T21:55:46Z")

</div>

oops… you really don’t need to specify ‘self’ in there, just ‘id’  
and ‘sometitle’ will do.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [August 27, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/restful-routes/147038/4 "2008-08-27T22:16:53Z")

</div>

Hi Dan,

I’m new to Rails myself, but I don’t think you have a restful issue  
here. I’ll make a couple of assumptions:  
You are calling the new page using a link\_to helper and you are  
calling the show action.

In your link\_to, you probably see a restful named route called  
something like pages\_path(title). The sometitle is an instance  
variable holding the ID field. You simply want to replace that with  
title.sometitle. That will provide you the url you want.

But, that is only the first half. The receiving action, presumably  
show, expects the ID, something like: Pages.find(params[:id]). You  
will need to change that to Pages.find\_by\_title(params[:title]  
[:sometitle], or something similar. This assumes title is a field in  
the table pages.

Hope this helps.

On Aug 27, 3:40 pm, Dan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [August 27, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/restful-routes/147038/5 "2008-08-27T23:00:16Z")

</div>

On Aug 27, 3:55 pm, Dan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Thanks guys, I’ll save this thread and refer to it again. I find a lot of things in Rails seem to take me a lot longer to understand than they should:)
> 
> Posted viahttp://www.ruby-forum.com/.

It’s great that you’re already trying to adhere to RESTful principles,  
though! Many newcomers to Rails don’t start that way and end up  
feeling the pain later.

Jeff

REST with Rails  
Oct 4, 2008, in Austin, TX  
$295 (get 10% off with discount code “PWRRGG”)  
[http://www.purpleworkshops.com/workshops/rest-and-web-services](http://www.purpleworkshops.com/workshops/rest-and-web-services)

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [August 27, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/restful-routes/147038/6 "2008-08-27T22:55:22Z")

</div>

Thanks guys, I’ll save this thread and refer to it again. I find a lot  
of things in Rails seem to take me a lot longer to understand than they  
should:)

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [August 28, 2008, 8:39am UTC](https://www.ruby-forum.com/t/restful-routes/147038/7 "2008-08-28T08:39:16Z")

</div>

rsl’s stringex library also does the same thing:

> **[GitHub - rsl/stringex: Some \[hopefully\] useful extensions to Ruby’s String...](https://github.com/rsl/stringex/tree/master)**
>
> master
