开发者

Username urls like Twitter, FriendFeed, Facebook, etc. in a Ruby on Rails project

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 r开发者_运维知识库esources 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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜