How has Github managed to get friendly URLs for representing repos of users? For a project called abc by username foo, how do they work around with a URL like: http://github.com/foo/abc. Are they fetching the abc model for the DB from the title in the URL (which sounds unreasonable as they are modifying the titles). How are they transferring the unique ID of the abc repo which they can fetch and show in the view?
The reason I ask is that I am facing a similar problem of creating friendlier URLs to view a resource. MongoDB’s object IDs are quite long and make the URL look horrific. Is there a workaround? All the tutorials that demonstrate CRUD (or REST) URLs for a resource always include the object’s unique ID(e.g. http://mysite.org/post/1 or http://mysite.org/post/1/edit. Is there a better way to do it?
I mocked a structure like this using FriendlyID and Nested Resources.
Essentially, use friendly ID to get the
to_param-ish slugs in your routes, then set up nested resources. Using GitHub as an example:routes.rb
Then in your controller, say, for repositories, you can check the existence of
params[:user_id]and use that to determine the user from the route. The reason I check for existence is because I did something like (roughly):So my controller does:
I followed this tutorial here to get started with this same project.