views:

278

answers:

2

Hi, I'm thinking about defining Restful urls for a new project (developed with Ruby On Rails). I like the simple and clean urls of the most famous web 2.0 services like "twitter.com/username". Not talking about problems in routing configuration (I want to do that with resources not using dirty "map.connect" overrides), the very problem is: how preserve words that you want to use for your pages (such as: website.com/help, website.com/api, etc.)? I don't want to think all the possibilities at time 0 (also because it is quite impossible).

A: 

Well, if you want to be on the safe side - think about a single prefix (map.with_options :prefix => 'do/') for all your custom stuff, so that all your controllers/actions will be prefixed like this

http://example.com/do/:controller/:action/:whatever

So you would just have to block the do as username and will have the freedom to do whatever you like after that prefix.

Marcel J.
Hi Marcel,yes I'm thinking about adding a short path, also possible using :as option (:as => :do in your example), but do you think that a correct RESTful resource must be semantically speaking for itself? For example if I'm talking about users is it correct to use the path http://example.com/do/username instead of http://example.com/users/username. I now that these are subtleties but why all the bigs of web 2.0 "complicate their lives" using urls like http://twitter.com/username?
zetarun
+1  A: 

Routes are prioritized by the moment they're declared. The first matching declared route will be the one executed.

So if you have :

map.resources :users
map.profile ':nickname', :controller => 'users', :action => 'show'

If you go to /users, you'll be on the map.resources :users route because that's the first one to match.
When you go to /zetarun, you'll be routed to map.profile ':nickname', :controller => 'users', :action => 'show' because that's the first route to match.

Then for the user profiles, you can do the following :

Define all your resources in the routes for all your controllers.
And at the end of the resources, you add the user profile resource.

So when a prefix is user by a controller, it's routed first by any route that exists.
And only the not routed url remains for user profiles.

Don't forget to add a "reserved nickname" validation on the user registration to avoid someone taking a nickname that won't give him a profile as there's something else on that url.

Damien MATHIEU
Hi,thanks for your response. It works but when I enter in someone profile and click a link (for example poiting to user images) I have the original resources link mapped in routes (i.e.: /users/zetarun/photos), if I want to change all the routes to begin only with /username, I have to add some other routing rules (in override at the resources rules). But what that made me paranoid is the "reserved nickname" validation...How can I know right now all the url that I want to use in the future (and blocking users taking these)...it's quite impossible, I know...
zetarun