Overriding std nested resources route

I have a set of nested resources as follows:

map.resources :users

map.resources :users, :has_many => :contacts

this gives me a std route as follows to list a user’s contacts in the
ContactsController ‘index’ action:

    /users/<user id>/contacts

A User has a ‘username’ and what I’d like to do is have the route/url
look like:

    /<username>/contacts

Does anyone know how to do this? I’ve tried using map.connect, but
that doesnt seem to work.

btw - I also tried

    map.resources :users, :path_prefix => "/:username"

but that still leaves the url as:

  /users/<username>/contacts

I’m trying to get rid of the ‘/users’ in the front.

Hi,

On Mon, 2009-08-10 at 17:46 -0700, lunaclaire wrote:

I’m trying to get rid of the ‘/users’ in the front.

I believe you need to consider using named routes.

HTH,
Bill

If you want the route to be www.mysite.com/bill-walton you will have
to create a catch-all and ofcourse that needs to go at the bottom of
your routes…

Something like so:

map.username ‘:username’, :controller => ‘users’, :action => ‘show’

But now you will have to do a good amount of security in the users
controller. All hackers will hit that controller if it does not match
any other routes… I guess you could add a regular expression but that
means your usernames would have to follow something that can be
regexed…

thx… I had tried named routes, but something must not have been
right… now I got it working as follows:

map.contacts ‘/:username/contacts’, :controller =>
‘contacts’, :action => ‘index’

this gives me what I wanted. if I use:

    contacts_url(@user.username):

I get:

    /<username>/contacts

That works for that one restful route, but I had hoped to be able to
do something with the nested resource declaration and have this apply
for all the routes… for instance I still have urls that look like
this:

    /users/<user id>/contacts/<contact id>

when I’d like to see:

    /<username>/contacts/<contact id>

anybody know how to get this form for all the generated restful
routes?