Getting vanity urls to work in rails is very simple. Let’s say you want to allow
your users to expose their Profiles through a facebook like url
http://www.facebook.com/GreenDay
. This is what you need to change in your
routes file.
The except => :show
on line#5 makes the resources helper skip the show route.
And line#9 which should be at the end of the routes file, creates a route called
profile which will be used for all the profile show links automatically.
That’s it now your application has vanity urls, whenever someone clicks on a
profile#show link they will be taken to /:slug. Obviously in this case the slug
is assumed to be unique.
1 |
|
###Update### One of the readers emailed me asking what the controller code would look like. Here it is:
1 |
|
###References###
- http://guides.rubyonrails.org/routing.html#naming-routes
- http://kconrails.com/2010/01/25/vanity-urls-for-ruby-on-rails-routes/