I want to be able to have twitter like friendly urls like http://twitter.com/username. How to pull this off in rails?
Its cool. However I was looking for something that does not expose the controller. Like in the example given as part of question description.
Chirantan
2009-09-25 12:48:29
+2
A:
Probably the easiest way is to have a catch-all route at the bottom of your routing table. Something like:
map.connect '/:slug', :controller => 'users', :action => 'show'
Then in the users controller....
def show
@user = User.find_by_username(params[:slug])
end
I would also recommend catching a ActiveRecord::RecordNotFound
to show a 404 page. You can put something like this in ApplicationController
:
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
then define a method not_found
to render an error page or something.
Ben
2009-09-25 13:16:06