# Topic title displaying in the URL

**URL:** <https://www.ruby-forum.com/t/topic-title-displaying-in-the-url/167728>\
**Category:** Rails\
**Created:** [May 12, 2009, 12:31am UTC](https://www.ruby-forum.com/t/topic-title-displaying-in-the-url/167728 "2009-05-12T00:31:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [May 12, 2009, 12:31am UTC](https://www.ruby-forum.com/t/topic-title-displaying-in-the-url/167728/1 "2009-05-12T00:31:22Z")

</div>

Hi

I would like to integrate the title string into a URL.

The standard is like:  
www.bla.xyz/topics/1

In view of a good search engine ranking I would like to write the title  
of a topic into the URL like:  
www.bla.xyz/topics/super-cool-topic

The problem are special characters like ‘?()&#’ etc. in the title  
string.

This is my route thinking:  
map.connect ‘:controller/:topictitle’, :controller =\> ‘topics’, :action  
=\> ‘show’

Now I need a revokable solution to turn the title into a suiteable form  
without special characters which could make problems.

Here is a function that can do the fitting, but it is not revokable, so  
I can’t do the Find command in the Controller (topic =  
Topic.find(:first, :conditions =\> [“title = ?”,  
topic\_title]))afterwards.

def topic\_to\_url(topic\_link)  
topic\_link.to\_s  
topic\_link = topic\_link.gsub(/[Ã¤]/, “ae”)  
topic\_link = topic\_link.gsub(/[Ã¼]/, “ue”)  
topic\_link = topic\_link.gsub(/[Ã¶]/, “oe”)  
topic\_link = topic\_link.gsub(" “, “-”)  
topic\_link = topic\_link.gsub(/[(,.?!;:’”)]/, “”)  
topic\_link.downcase  
end

Is there a better way to do the integration?  
Is there a function to convert strings into url suitable forms which can  
undo the hole thing afterwards?  
Should I commit also the ID?

I would love to here how you do that…

Greeting, Chaos

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [May 12, 2009, 1:42am UTC](https://www.ruby-forum.com/t/topic-title-displaying-in-the-url/167728/2 "2009-05-12T01:42:44Z")

</div>

> The problem are special characters like ‘?()&#’ etc. in the title
> 
> topic\_link = topic\_link.gsub(/[ö]/, “oe”)
> 
> I would love to here how you do that…

Take a look at the to\_url method of

> **[GitHub - rsl/stringex: Some \[hopefully\] useful extensions to Ruby’s String...](https://github.com/rsl/stringex/tree/master)**
>
> master

It handles all of this for you.

Then, if it were me, put your route back to it’s default of using :id  
and then in your Topic model do:

def to\_param  
“#{id} #{title}”.to\_url  
rescue  
id  
end

That will have the affect that for the most part you will get nice  
urls like ‘/topics/123-super-cool-topic’ but in the worst case you’ll  
still get the id of 123. So in your controller you can simply do:

Topic.find\_by\_id(params[:id].to\_i) # to\_i will strip everything after  
the dash leaving you with just the id.

-philip
