# Named - restful routes question

**URL:** <https://www.ruby-forum.com/t/named-restful-routes-question/156297>\
**Category:** Rails\
**Created:** [December 24, 2008, 3:12am UTC](https://www.ruby-forum.com/t/named-restful-routes-question/156297 "2008-12-24T03:12:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [December 24, 2008, 3:12am UTC](https://www.ruby-forum.com/t/named-restful-routes-question/156297/1 "2008-12-24T03:12:34Z")

</div>

hi. in order to better understand restful routes, i started with simple  
routes and named routes. before doing map.resources :users in my app, i  
decide to simulate all the 7 possibilities in my routes,rb file:

# map.resources :users

map.users ‘users’, :controller =\> “users”, :action  
=\> ‘index’  
map.users ‘users/create’, :controller =\> “users”, :action  
=\> ‘create’, :method =\> :post  
map.new\_user ‘users/new’, :controller =\> “users”, :action  
=\> ‘new’  
map.edit\_user ‘users/:id/edit’, :controller =\> “users”, :action  
=\> ‘edit’  
map.user ‘users/:id’, :controller =\> “users”, :action  
=\> ‘show’  
map.user ‘users/:id’, :controller =\> “users”, :action  
=\> ‘update’, :method =\> :put  
map.user ‘users/:id’, :controller =\> “users”, :action  
=\> ‘destroy’, :method =\> :delete

my index.html.erb file has the following code to create the links for  
show, edit and delete:

```
<td><%= link_to "show", user %></td>
<td><%= link_to "edit", edit_user_path(user) %></td>
<td><%= link_to 'delete', user,
                            :confirm => 'Confirm delete?',
                            :method => :delete %></td>

```

but when i click on the delete link and confirm the delete javascript  
alert, the show action is triggered instead of the destroy.

any ideas where is my mistake?

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [December 24, 2008, 4:06am UTC](https://www.ruby-forum.com/t/named-restful-routes-question/156297/2 "2008-12-24T04:06:54Z")

</div>

On Dec 23, 2008, at 6:12 PM, Soh D. \<rails-mailing-list@andreas-  
[s.net](http://s.net)\> wrote:

> =\> ‘index’  
> map.user ‘users/:id’, :controller =\> “users”, :action
> 
> but when i click on the delete link and confirm the delete javascript  
> alert, the show action is triggered instead of the destroy.
> 
> any ideas where is my mistake?

Hi, try using user\_path( user ) instead of user.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [December 24, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/named-restful-routes-question/156297/3 "2008-12-24T17:01:17Z")

</div>

Conrad T. wrote:

> Hi, try using user\_path( user ) instead of user.

I tested with user\_path(user), but didn’t solve the problem.

If I stack something like:  
map.user … (show)  
map.user … (update)  
map.user … (destroy)  
Then the top first match should take place, which is the (show)

The reason I did that is because … suppose I just have map.resources  
users inside the routes.rb file and then I rake routes in the terminal  
the result will be:

```
users GET /users {:action=>"index", 

```

:controller=\>“users”}  
POST /users {:action=\>“create”,  
:controller=\>“users”}  
new\_user GET /users/new {:action=\>“new”,  
:controller=\>“users”}  
edit\_user GET /users/:id/edit {:action=\>“edit”,  
:controller=\>“users”}  
user GET /users/:id {:action=\>“show”,  
:controller=\>“users”}  
PUT /users/:id {:action=\>“update”,  
:controller=\>“users”}  
DELETE /users/:id  
{:action=\>“destroy”,:controller=\>“users”}

And if you look at the last 3 paths you have user for GET, PUT and  
DELETE, thats why I thought I should use just user instead of user\_show,  
user\_update and user\_delete and rails should know which one is the right  
one by looking at the routes.rb and in the view. Well, appears that it  
doesn’t work that way, but why it does work with map.resources? I may be  
missing something else?

---

<div class="post-metadata">

**Author:** ![sohdubom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sohdubom](https://www.ruby-forum.com/u/sohdubom)\
**Post date:** [December 27, 2008, 10:34am UTC](https://www.ruby-forum.com/t/named-restful-routes-question/156297/4 "2008-12-27T10:34:47Z")

</div>

well, answering my own doubt, once one decide to comment:

# map.resources users

and then use named paths … you will have to differentiate the names  
like:

map.user\_show …  
map.user\_update …  
map.user\_delete …

why? because paths like that and also with simple paths, rails will be  
reading and executing the routes.rb file with TOP\_FIRST priority and  
that’s the rule!

in other words, the rake routes output, eg:

user GET /users/:id {:action=\>“show”,  
:controller=\>“users”}  
PUT /users/:id {:action=\>“update”,  
:controller=\>“users”}  
DELETE /users/:id {:action=\>“destroy”,  
:controller=\>“users”}

that output does NOT imply you can use same name paths and differentiate  
then with different :methods!
