# Question about named route

**URL:** <https://www.ruby-forum.com/t/question-about-named-route/186917>\
**Category:** Rails\
**Created:** [April 14, 2010, 10:21am UTC](https://www.ruby-forum.com/t/question-about-named-route/186917 "2010-04-14T10:21:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [April 14, 2010, 10:21am UTC](https://www.ruby-forum.com/t/question-about-named-route/186917/1 "2010-04-14T10:21:17Z")

</div>

Hi,

I have quetion about ‘named route’.

I defined the following in routes.rb to use ‘:title’ instead of ‘:id’.

map.movie ‘/  
movies/:title’, :controller=\>‘movies’, :action=\>‘show’,  
:conditions=\>{:method=\>:get}

This seems to work fine, but ‘movie\_path()’ still uses ‘:id’ instead  
of ‘:title’.

movie = Movie.get(parms[:id])  
p movie\_path(movie) #=\> “/movies/1” (expected is “/movies/Avatar”)

How to change ‘movie\_path()’ to use ‘:title’?

–  
regards,  
makoto

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [April 14, 2010, 10:30am UTC](https://www.ruby-forum.com/t/question-about-named-route/186917/2 "2010-04-14T10:30:39Z")

</div>

you can use  
[http://svn.techno-weenie.net/projects/plugins/permalink\_fu/plugin](http://svn.techno-weenie.net/projects/plugins/permalink_fu/plugin)

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [April 14, 2010, 10:35am UTC](https://www.ruby-forum.com/t/question-about-named-route/186917/3 "2010-04-14T10:35:56Z")

</div>

> movie = Movie.get(parms[:id])  
> p movie\_path(movie) #=\> “/movies/1” (expected is “/movies/Avatar”)
> 
> How to change ‘movie\_path()’ to use ‘:title’?

The reason this happens is that movie\_path converts an object to a  
parameter  
using the to\_param method of the object, rather than knowing what  
parameter  
the route wants.

So, there are two solutions:

1. 

class Movie  
def to\_param  
name  
end  
end

1. 

movie\_path(movie.title)

Depending on how many other paths rely on you passing a movie around,  
that  
will affect your decision. A little known third option is this:

class Movie  
def to\_param  
“#{id}-#{name}”  
end  
end

That way you can just use the param :id in your path (and 1-Avatar will  
be  
converted to the integer 1 when searched using the :id field) but the  
title  
of the movie will be in the URLs for SEO purposes.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [April 14, 2010, 10:38am UTC](https://www.ruby-forum.com/t/question-about-named-route/186917/4 "2010-04-14T10:38:51Z")

</div>

On 14 April 2010 09:29, bala kishore pulicherla [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> you can use [http://svn.techno-weenie.net/projects/plugins/permalink\_fu/plugin](http://svn.techno-weenie.net/projects/plugins/permalink_fu/plugin)

I think you’re solving a different problem than he asked for though.  
Permalink\_fu is good for creating slugs, so if he had “Throw Momma From  
The  
Train” as a movie it would stop URLs like this:

“[http://example.com/movies/Throw](http://example.com/movies/Throw) Momma From The Train”

It’s a good point, using the title/name of the movie (that may have  
spaces)  
will break sooner or later, but he was asking about ids/names in  
parameters  
for named\_routes.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![makoto\_kuwata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a4c791/32.png) [@makoto\_kuwata](https://www.ruby-forum.com/u/makoto_kuwata)\
**Post date:** [April 14, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/question-about-named-route/186917/5 "2010-04-14T16:47:06Z")

</div>

bala and Andy,  
Thank you very much!

I choosed to override to\_param() as “#{id}-#{name}”.

## Thank you.

regards,  
makoto
