Wabbitseason
Wabbitseason

Reputation: 5691

How to use slugs in RESTful URLs?

According to current best practices a RESTful URL for a given thread on a message board should look something like this:

http://domain/forum/threads/3

It is also a common SEO practice that URLs should contain keywords (slug), so perhaps the above URL could become:

http://domain/forum/threads/3/title-of-this-particular-thread

Now, to edit this thread, again, according to the guidelines I've linked to in the first paragraph, the URL would be:

http://domain/forum/threads/3/edit

What should happen when someone starts a thread with the title of "edit"? How should it be decided if the thread is to be shown or edited?

Upvotes: 0

Views: 1348

Answers (1)

Peter Brown
Peter Brown

Reputation: 51717

Instead of http://domain/forum/threads/3/title-of-this-particular-thread

You should be doing http://domain/forum/threads/3-title-of-this-particular-thread

This will prevent conflicts and is just as SEO friendly. There are a few ways to accomplish this, but the easiest is to add a to_param method in your model that does the conversion automatically:

class Thread < ActiveRecord::Base
  to_param
    "#{id}-#{title}"
  end
end

If you need more flexibility than this, or don't want to repeat it in all your models, you can use the friendly_id Gem.

Upvotes: 1

Related Questions