# RESTful and pretty URLs

**URL:** <https://www.ruby-forum.com/t/restful-and-pretty-urls/81346>\
**Category:** Rails\
**Created:** [January 8, 2007, 4:26am UTC](https://www.ruby-forum.com/t/restful-and-pretty-urls/81346 "2007-01-08T04:26:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 8, 2007, 4:26am UTC](https://www.ruby-forum.com/t/restful-and-pretty-urls/81346/1 "2007-01-08T04:26:55Z")

</div>

I’ve switched over to RESTful behavior on my site I am working on.

A URL like this works fine:

/make/1/model/2/year/1995

Assuming that make.find(1).name is ‘Ford’ and model.find(2).name is  
‘Taurus’ (year is obvious) is it possible to be RESTful and still have  
pretty URLs? Such as:

/vehicle/Ford/Taurus/1995

Of course this works in the old scheme with something like:

map.vehicle vehicle/:make\_name/:model\_name/:year\_id, :controller =\>  
‘vehicle’, :action =\> ‘show’

Thanks!

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 8, 2007, 1:37pm UTC](https://www.ruby-forum.com/t/restful-and-pretty-urls/81346/2 "2007-01-08T13:37:28Z")

</div>

Part of the solution [the easy part] would be to implement to\_param in  
your  
models to yield the product name/make rather than the id. I’m just not  
sure  
how to eliminate the words make, model, and year. Maybe someone else  
will  
respond to that. I’m a RESTful noob myself.

RSL

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 8, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/restful-and-pretty-urls/81346/3 "2007-01-08T19:38:52Z")

</div>

I can live with the controller words in the URL if I have to.

I was not aware of to\_param. The docs don’t seem to really talk about  
to\_param in this way and from reading a blog post or two it would seem  
to be “black magic”. Not sure I want to go there…

As far as I can tell the URL naming flexibility is just not available  
when RESTful, like it is with classic routing. That’s unfortunate.

Russell N. wrote:

> Part of the solution [the easy part] would be to implement to\_param in  
> your  
> models to yield the product name/make rather than the id. I’m just not  
> sure  
> how to eliminate the words make, model, and year. Maybe someone else  
> will  
> respond to that. I’m a RESTful noob myself.
> 
> RSL

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 8, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/restful-and-pretty-urls/81346/4 "2007-01-08T19:55:34Z")

</div>

If you want an idea on how to use to\_param, check out the  
acts\_as\_sluggable.  
I believe the peepcode RESTful movie also goes over using to\_param, but  
I  
could be incorrect.

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 9, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/restful-and-pretty-urls/81346/5 "2007-01-09T21:26:43Z")

</div>

On 1/8/07, Carl J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can live with the controller words in the URL if I have to.
> 
> I was not aware of to\_param. The docs don’t seem to really talk about  
> to\_param in this way and from reading a blog post or two it would seem  
> to be “black magic”. Not sure I want to go there…
> 
> As far as I can tell the URL naming flexibility is just not available  
> when RESTful, like it is with classic routing. That’s unfortunate.

Of course it is flexible. REST is about creating resources and making  
them usable with the HTTP verbs. That’s really all there is to it.  
Using map.resources doesn’t make your app RESTful, and not using  
map.resources doesn’t preclude you from making your app RESTful. It  
just automates a lot of the routing for you so it’s easier to get  
where you want to be.

Pat

---

<div class="post-metadata">

**Author:** ![cjnkns](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c5a1d2/32.png) [@cjnkns](https://www.ruby-forum.com/u/cjnkns)\
**Post date:** [January 9, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/restful-and-pretty-urls/81346/6 "2007-01-09T22:01:40Z")

</div>

> Of course it is flexible. REST is about creating resources and making  
> them usable with the HTTP verbs. That’s really all there is to it.

Sorry, I should clarify. REST is flexible. I could create my own RESTful  
routes and links, of course.

map.resources is not flexible in one area - it only accepts :id. As far  
as I can tell.
