# Routing title in url

**URL:** <https://www.ruby-forum.com/t/routing-title-in-url/51608>\
**Category:** Rails\
**Created:** [January 4, 2006, 8:21am UTC](https://www.ruby-forum.com/t/routing-title-in-url/51608 "2006-01-04T08:21:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chillypepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chillypepper](https://www.ruby-forum.com/u/chillypepper)\
**Post date:** [January 4, 2006, 8:21am UTC](https://www.ruby-forum.com/t/routing-title-in-url/51608/1 "2006-01-04T08:21:19Z")

</div>

I’ve set up my app to display a post by retrieving its title instead of  
id, which works fine, except it looks like this  
“/post/the+title+of+post%3F” and I want it to be “the\_title\_of\_post?”  
with underscores instead of plusses. Can anyone shed light?

---

<div class="post-metadata">

**Author:** ![chillypepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chillypepper](https://www.ruby-forum.com/u/chillypepper)\
**Post date:** [January 4, 2006, 8:38am UTC](https://www.ruby-forum.com/t/routing-title-in-url/51608/2 "2006-01-04T08:38:38Z")

</div>

On Wed, Jan 04, 2006 at 08:21:19AM +0100, Kyle Jones wrote:

> I’ve set up my app to display a post by retrieving its title instead of  
> id, which works fine, except it looks like this  
> “/post/the+title+of+post%3F” and I want it to be “the\_title\_of\_post?”  
> with underscores instead of plusses. Can anyone shed light?

You could define Post#to\_param like this:

class Post \< ActiveRecord::Base  
def to\_param  
title.gsub(/\s+/, ‘\_’)  
end  
end

Then:

link\_to @post.title, :action =\> ‘show’, :id =\> @post

or

link\_to @post.title, show\_post\_url(:id =\> @post)

to\_param will be called implicitly.

marcel

---

<div class="post-metadata">

**Author:** ![chillypepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chillypepper](https://www.ruby-forum.com/u/chillypepper)\
**Post date:** [January 4, 2006, 9:01am UTC](https://www.ruby-forum.com/t/routing-title-in-url/51608/3 "2006-01-04T09:01:22Z")

</div>

That works at least to create the desired URL, but now i still have to  
find\_by\_post with it, and it doesnt like the new underscores. also how  
would you retain characters like “?” instead of “%3F” ?

---

<div class="post-metadata">

**Author:** ![chillypepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chillypepper](https://www.ruby-forum.com/u/chillypepper)\
**Post date:** [January 4, 2006, 9:02am UTC](https://www.ruby-forum.com/t/routing-title-in-url/51608/4 "2006-01-04T09:02:05Z")

</div>

Kyle Jones wrote:

> That works at least to create the desired URL, but now i still have to  
> find\_by\_post with it, and it doesnt like the new underscores. also how  
> would you retain characters like “?” instead of “%3F” ?

find\_by\_title my bad 😛

---

<div class="post-metadata">

**Author:** ![chillypepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chillypepper](https://www.ruby-forum.com/u/chillypepper)\
**Post date:** [December 30, 2006, 9:20am UTC](https://www.ruby-forum.com/t/routing-title-in-url/51608/5 "2006-12-30T09:20:07Z")

</div>

Kyle Jones wrote:

> I’ve set up my app to display a post by retrieving its title instead of  
> id, which works fine, except it looks like this  
> “/post/the+title+of+post%3F” and I want it to be “the\_title\_of\_post?”  
> with underscores instead of plusses. Can anyone shed light?

maybe the plugin acts\_as\_urlnameable from  
[http://gironda.org/acts\_as\_urlnameable/](http://gironda.org/acts_as_urlnameable/)  
could help you.

---

<div class="post-metadata">

**Author:** ![chillypepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@chillypepper](https://www.ruby-forum.com/u/chillypepper)\
**Post date:** [December 30, 2006, 9:33am UTC](https://www.ruby-forum.com/t/routing-title-in-url/51608/6 "2006-12-30T09:33:21Z")

</div>

or even acts\_as\_sluggable. [http://dev.2750flesk.com/acts\_as\_sluggable/](http://dev.2750flesk.com/acts_as_sluggable/)
