# Reverse formatting routes...?

**URL:** <https://www.ruby-forum.com/t/reverse-formatting-routes/129872>\
**Category:** Rails\
**Created:** [February 22, 2008, 8:11am UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872 "2008-02-22T08:11:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 8:11am UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/1 "2008-02-22T08:11:47Z")

</div>

i have this set up  
map.connect ‘startrekvoyager/episodes’, :controller =\>  
‘categories’, :action =\> ‘show’, :id =\> 2

but when i call  
link\_to “Voyager”, :controller =\> :categories, :action =\> :show, :id  
=\> 2

it comes up as  
[http://brocoum.com/voter/categories/show/2](http://brocoum.com/voter/categories/show/2)

i want it to come up as  
[http://brocoum.com/voter/startrekvoyager/episodes](http://brocoum.com/voter/startrekvoyager/episodes)

just so it’s more readable

is there a way to do this “reverse” route lookup?

Thanks!

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 8:36am UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/2 "2008-02-22T08:36:31Z")

</div>

On Feb 22, 2008, at 2:11 AM, Stedwick wrote:

> i want it to come up as  
> [http://brocoum.com/voter/startrekvoyager/episodes](http://brocoum.com/voter/startrekvoyager/episodes)
> 
> just so it’s more readable
> 
> is there a way to do this “reverse” route lookup?
> 
> Thanks!

Try removing the default route or placing it after the route you wish  
to have precedence.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/3 "2008-02-22T17:56:50Z")

</div>

Arranging it hasnt worked, and i cant get rid of the defaults because  
ill need them for other things. Heres my routes:

map.home ‘’, :controller =\> ‘categories’

map.connect ‘startrekvoyager/episodes’, :controller =\>  
‘categories’, :action =\> ‘show’, :id =\> 2  
map.connect ‘startrekdeepspacenine/episodes’, :controller =\>  
‘categories’, :action =\> ‘show’, :id =\> 4

map.connect ‘startrekvoyager/characters’, :controller =\>  
‘categories’, :action =\> ‘show’, :id =\> 5  
map.connect ‘startrekdeepspacenine/characters’, :controller =\>  
‘categories’, :action =\> ‘show’, :id =\> 6

# Allow downloading Web Service WSDL as a file with an extension

# instead of a file named ‘wsdl’

map.connect ‘:controller/service.wsdl’, :action =\> ‘wsdl’

# Install the default route as the lowest priority.

map.connect ‘:controller/:action/:id.:format’  
map.connect ‘:controller/:action/:id’

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/4 "2008-02-22T18:25:07Z")

</div>

On Feb 22, 2008, at 11:55 AM, Stedwick wrote:

> Arranging it hasnt worked, and i cant get rid of the defaults because  
> ill need them for other things. Heres my routes:

Well, you can eliminate the need for default routes 😉

Seriously, use map.resources or

with\_options :controller =\> ‘some’ do |s|  
s.bar ‘foo/bar’, :action =\> ‘show’, :id =\> 1  
s.baz ‘foo/baz’, :action =\> ‘show’, :id =\> 2  
end

and make sure there’s only one possible match for a given set of  
parameters.

If you use named routes rather than map.connect everywhere, you can:

map.stv\_episodes(‘startrekvoyager/episodes’,  
:controller =\> ‘categories’,  
:action =\> ‘show’, :id =\> 2)

link\_to “Star Trek Voyager: Episodes”, stv\_episodes\_path

rather than specifying the id (which ties you to the particulars of  
the data that I would personally avoid, but at least the change would  
be limited to your routes file).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/5 "2008-02-22T19:05:19Z")

</div>

Two basic options. First you can create a named route:

map.\<route\_name\> ‘route’, :controller=\>…

That way you can build your link\_to’s, etc using \<route\_name\>\_url and  
\<route\_name\>\_path. You’ll probably have to ditch the forward slash in  
favor or a dash, however.

Second, you can override the ‘to\_param’ method of ActiveRecord::Base.  
By default it returns the id of the record but you can tell it to  
return anything… including text. With that in mind I’d suggest  
something like this:

class TelevisionSeries  
has\_many :episodes  
has\_many :characters

def to\_param  
name.gsub(/\s+/, ‘\_’).dasherize  
end

…  
end

With that you can use the default RESTfully named routes if you also  
nest the routes like so:

map.resources :television\_series |tv|  
tv.has\_many :episodes  
tv.has\_many :characters  
end

Which will result in routes like this:

[http://localhost:3000/television\_series/deep-space-nine/episodes](http://localhost:3000/television_series/deep-space-nine/episodes)  
[http://localhost:3000/television\_series/deep-space-nine/characters](http://localhost:3000/television_series/deep-space-nine/characters)

The last thing to remember is that you’ll receive the dasherized name  
of the series in params[:id] so you’ll have to operate on it before  
attempting to find… and you’ll use find\_by\_name.

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/6 "2008-02-22T20:20:27Z")

</div>

On Feb 22, 2008, at 12:50 PM, Stedwick wrote:

> The entire backend is simply an acts\_as\_tree Category model and an  
> Item model. Each category has subcategories, and the subcategories  
> have items. Who knows, I might want to vote on fruit one day 🙂
> 
> Anyway, a named route like st\_voy\_url will never work because I will  
> never manually link to it. My index page simply iterates over all  
> categories with items and spits out an unordered list.
> 
> Does it make sense what I’m saying and what I’m trying to do?

Well, I don’t think you’ll ever be able to get URLs to look as nice  
that way, but I see what you mean. Have you considered overriding  
#to\_param in your model to be something like a permalink? At least  
then your URL would be something like:

[http://www.brocoum.com/voter/categories/show/2-star-trek-voyager-episodes](http://www.brocoum.com/voter/categories/show/2-star-trek-voyager-episodes)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/7 "2008-02-22T18:51:41Z")

</div>

The reason I can’t use named routes is because I’m writing my “index”  
page.

You can see it here.  
[http://www.brocoum.com/voter/](http://www.brocoum.com/voter/)

Also, everything is stored generically in my database. I wanted it to  
be as flexible as possible. In other words, not everything is going to  
be Star Trek, or a TV show, or have characters and episodes.

The entire backend is simply an acts\_as\_tree Category model and an  
Item model. Each category has subcategories, and the subcategories  
have items. Who knows, I might want to vote on fruit one day 🙂

Anyway, a named route like st\_voy\_url will never work because I will  
never manually link to it. My index page simply iterates over all  
categories with items and spits out an unordered list.

Does it make sense what I’m saying and what I’m trying to do?

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [February 22, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/reverse-formatting-routes/129872/8 "2008-02-22T20:35:13Z")

</div>

> removed\_email\_address@domain.invalid  
> whoa, that link URL actually works 🙂 I guess you can put anything  
> you want after the number.

thanks for all the help you guys, I’m not sure what I’ll do, but it’s  
not really a big deal anyways. I might just use named routes and  
manually link them.
