April 10, 2007

Pretty RESTful URLs in Rails

Since Release 1.2 Rails knows to generate RESTful routes. Each resource is identified by an URI, which looks like /users/123 . It would be nice to have more readable URLs, which include the name of the user: /users/matthias-georgi. This is a short tutorial on making your urls pretty while retaining the REST approach. Assuming that you already have an user model, we add following line to our config/routes.rb:

map.resources :users
You may now run the scaffold_resource generator in case you don't have any UsersController yet. In order to remember the permalink for each user, we store it in a column named permalink in the users table. Before we save an user record, we have to infer a url-safe permalink for the user name. We do this by:
def before_save
  self.permalink = name.downcase.gsub(/[^a-z0-9]+/i, '-') if permalink.blank?
end
Each character, which is not an alphanumeric will be replaced by a dash. This is only done, if the permalink is not set already. So we have a way for users to set their permalink manually. To avoid duplicate permalinks, we validate the uniqeness of the permalink:
validates_uniqeness_of :permalink
What happens now, if we browse to a user url like /users/matthias-georgi ? Rails raises an exception, telling us that it cannot find an record with the id matthias-georgi. We'll fix now our controller to look for the permalink and not the id of the user. Just replace each call to User.find(params[:id]) with User.find_by_permalink(params[:id]). Also we have to ensure, that our user routes will be generated correctly. Therefore we overwrite the to_param method:
def to_param
 permalink
end
The effect of this little change is, that user_url(a_user) generates the right url. Nested resources and pretty urls can get tricky and for now I won't touch this topic. Remember that changing the permalink may be problematic as links to the old url will get invalid. Nevertheless have fun experimenting with pretty urls and the new wonderful world of REST.

8 comments:

Aníbal Rojas said...

I must agree that retsful urls make more sense when the pattern used to address the resource is meaningful. And while reading your post, I could only think... "What about nested resources?" ;-)

Matthias Georgi said...

For nested resources your controller gets multiple parameters for identifying a resource. For example /users/matthias-georgi/articles/my-blog-post
yields the user name and the title of the article. So you must ensure the uniqueness of an article title in the scope of one user.

Robby Russell said...

Here is an example with nested resources.

Benjamin Curtis said...

You might find this plugin useful, as it does it all for you. :)

http://agilewebdevelopment.com/plugins/friendly_identifier

aventura-nikolsburg said...

I have used this technique before to good effect, however I just ran into the problem that if you set up a sweeper to expire cashed pages, the sweeper seems to insist on the numeric ids. For example I have the following url: /countries/us/regions/ma. Now if that page needs to be expired, the sweeper will look for /countries/1/regions/96 which of course means that page won't expire. I am looking for a way around this...

Ben said...

Hi,

I think

User.find_by_permalink(params[:id])

should be

User.find_by_permalink(params[:permalink])

Ben said...

Something which is going wrong here is this:

validates_uniqueness_of :permalink

The uniqueness of the permalink is evaluated by rails *before* the permalink is set using your callback.

So this will not preclude that you use a permalink which is already been taken...

ynw said...

good point ben