The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Pretty RESTful URLs in Rails

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Matthias Georgi

Posts: 54
Nickname: georgi
Registered: Apr, 2007

Matthias Georgi is a Ruby on Rails freelancer.
Pretty RESTful URLs in Rails Posted: Apr 10, 2007 5:00 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Matthias Georgi.
Original Post: Pretty RESTful URLs in Rails
Feed Title: Matthias Georgi
Feed URL: http://feeds.feedburner.com/matthias-georgi?format=xml
Feed Description: Webdev, Gamedev and Interaction Design.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Matthias Georgi
Latest Posts From Matthias Georgi

Advertisement
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.

Read: Pretty RESTful URLs in Rails

Topic: Unbelievable mess on my tumblelog Previous Topic   Next Topic Topic: Building a del.ico.us and flickr sidebar in 5 minutes

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use