# Overriding std nested resources route

**URL:** <https://www.ruby-forum.com/t/overriding-std-nested-resources-route/173528>\
**Category:** Rails\
**Created:** [August 11, 2009, 2:32am UTC](https://www.ruby-forum.com/t/overriding-std-nested-resources-route/173528 "2009-08-11T02:32:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 11, 2009, 2:32am UTC](https://www.ruby-forum.com/t/overriding-std-nested-resources-route/173528/1 "2009-08-11T02:32:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 11, 2009, 2:47am UTC](https://www.ruby-forum.com/t/overriding-std-nested-resources-route/173528/2 "2009-08-11T02:47:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 11, 2009, 2:57am UTC](https://www.ruby-forum.com/t/overriding-std-nested-resources-route/173528/3 "2009-08-11T02:57:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 11, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/overriding-std-nested-resources-route/173528/4 "2009-08-11T18:17:37Z")

</div>

If you want the route to be [www.mysite.com/bill-walton](http://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…

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [August 12, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/overriding-std-nested-resources-route/173528/5 "2009-08-12T23:45:29Z")

</div>

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?
