# Simplifying RESTful urls

**URL:** <https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197>\
**Category:** rubyonrails-talk\
**Created:** [August 20, 2007, 9:54am UTC](https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197 "2007-08-20T09:54:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DAZ](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DAZ](https://discuss.rubyonrails.org/u/DAZ)\
**Post date:** [August 20, 2007, 9:54am UTC](https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197/1 "2007-08-20T09:54:27Z")

</div>

Hi, If I am making an application that has a programmes model which has many episodes, and use nested resourses like so:

map.resources :programmes do |programmes| &nbsp;&nbsp;programmes.resources :episodes end

This then produces urls like so:

/programmes/4/episodes/5

If I then use permalins, I can get them to look like this:

/programmes/simpsons/episodes/bart-the-general

Is there any way of removing the references to the models in the url? So the url would look like:

/simpsons/bart-the-general

Thanks,

DAZ

---

<div class="post-metadata">

**Author:** ![Jeremy\_McAnally](https://avatars.discourse-cdn.com/v4/letter/j/c89c15/32.png) [@Jeremy\_McAnally](https://discuss.rubyonrails.org/u/Jeremy_McAnally)\
**Post date:** [August 20, 2007, 3:03pm UTC](https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197/2 "2007-08-20T15:03:23Z")

</div>

Don't use the built-in RESTful routing. 🙂 Or define your own custom routes:

map.connect ':show\_name/:episode\_id', :controller =\> 'episodes', :action =\> 'show'

I'm not sure that's a valid route, but it would look something like that. In theory, that would minimize the amount of code you'd need to change in your controllers.

--Jeremy

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://discuss.rubyonrails.org/u/David_A_Black1)\
**Post date:** [August 20, 2007, 3:12pm UTC](https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197/3 "2007-08-20T15:12:56Z")

</div>

Hi --

> > Hi, If I am making an application that has a programmes model which has many episodes, and use nested resourses like so:
> > 
> > map.resources :programmes do |programmes| &nbsp;&nbsp;programmes.resources :episodes end
> > 
> > This then produces urls like so:
> > 
> > /programmes/4/episodes/5
> > 
> > If I then use permalins, I can get them to look like this:
> > 
> > /programmes/simpsons/episodes/bart-the-general
> > 
> > Is there any way of removing the references to the models in the url? So the url would look like:
> > 
> > /simpsons/bart-the-general
> 
> Don't use the built-in RESTful routing. 🙂 Or define your own custom routes:
> 
> map.connect ':show\_name/:episode\_id', :controller =\> 'episodes', :action =\> 'show'
> 
> I'm not sure that's a valid route, but it would look something like that. In theory, that would minimize the amount of code you'd need to change in your controllers.

It's valid route, but it's a bit "greedy", in the sense that if you also had:

&nbsp;&nbsp;&nbsp;map.connect ':actor\_name/:movie\_id'

or something, you'd have two ':wildcard/:wildcard' routes, of which the first to be defined would "win" for all 'x/y' URLs. This may or may not become a problem in a given application, but I've found that it does tend to become one 🙂

You could tweak it with regex matching for the various wildcards, if that's appropriate.

David

---

<div class="post-metadata">

**Author:** ![AndyV1](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@AndyV1](https://discuss.rubyonrails.org/u/AndyV1)\
**Post date:** [August 20, 2007, 3:59pm UTC](https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197/4 "2007-08-20T15:59:06Z")

</div>

Not only that, but the routes that include the resource names might be very handy with respect to SEO. That is, you might get higher on a list for someone searching for a "Simpsons episode..." if your url includes both.

If using the name, rather than the id, is something that you can commit to as a universal design decision for your urls then there's a pretty easy way to make the pretty urls without changing the routing. ActiveRecord objects make use of the 'to\_param' method when they are used as parameters to a method call. By default they return the record id. That's why the call:

episode\_path(@simpsons, @bart\_the\_general)

comes out like /programmes/4/episodes/5

If you simply override the method then you can use the same routing but the names instead:

class Programme &nbsp;&nbsp;... &nbsp;&nbsp;def to\_param &nbsp;&nbsp;&nbsp;&nbsp;self.name.dasherize &nbsp;&nbsp;end end

class Episode &nbsp;&nbsp;... &nbsp;&nbsp;def to\_param &nbsp;&nbsp;&nbsp;&nbsp;self.name.dasherize &nbsp;&nbsp;end end

(Having written that twice I see a refactoring coming on... 🙂

Now your call to episode\_path should produce:

/programmes/simpsons/episode/bart-the-general

The remaining work to be done is to update your controllers, making them capable of finding the objects based on the dasherized name.

HTH, AndyV

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 11, 2008, 9:38am UTC](https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197/5 "2008-02-11T09:38:49Z")

</div>

I m looking for the same...

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 11, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/simplifying-restful-urls/17197/6 "2008-02-11T09:40:22Z")

</div>

Naresh Rana wrote:

> I m looking for the same...

/domain/action/id/title

i want to make my url look likie this,

/domain/action/title
