Skip to main content

Friendly URL's question

Gems / Libraries • Asked by Nelson Casanova


I'm building a small forum for my website and its a very simple app with the schema looking a bit like:

Topic -> Forum -> Post

So, Topic can be 'Off-Topic' with a Forum called 'Introductions' and maybe a post titled 'Hi There'

In an ideal world the index is a list of Topics and each one displaying a link to its underlying Forums. Once you click a forum URL will look like: and once I click a post the URL looks like

Currently what I've got looks more like feels like if I implement the friendly_id gem I will still end up with something like How can I achieve a url that does not include the model in it?

Right, got it. I just hope this is not going to cause major headaches in the future.

I installed friendly_id and add the gem functionality to the topic, forum and post models.

As expected I ended up with a url that looked like not bad but still not optimal.

I added the following to my routes.rb:

resources :forums, :path => '' do
   resources :posts, :path => '' do

Done. Like i said, I hope this is the right way...

Hey Nelson,

The name of the model helps make routing way easier, so that's why it's done that way. Here's an example of why.

You might have a /sign_up route, but if you weren't careful, "sign_up" can easily get treated as a topic, which you don't want.

To do this, you'll just write your own custom routes.

get "/sign_up"

get "/:topic/:forum/:post"
get "/:topic/:forum"
get "/:topic"

This way, sign up should get interpreted first, instead of it being treated as a topic. And the rest of the routes are catch-alls to treat everything else as if it were a topic and so on. Those routes have to be the last in your routes file in order to not override the other routes.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.