Forum: Ruby on Rails Topic title displaying in the URL

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5c087c6a1b61f6080ed2bc81787e0fbc?d=identicon&s=25 Andi Geheim (chaosbanane)
on 2009-05-12 00:31

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

The standard is like:

In view of a good search engine ranking I would like to write the title
of a topic into the URL like:

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

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 = ?",

def topic_to_url(topic_link)
    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(/[(,.?!;:'")]/, "")

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
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-05-12 01:42
(Received via mailing list)
> 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
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

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.

This topic is locked and can not be replied to.