Named - restful routes question

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?

On Dec 23, 2008, at 6:12 PM, Soh D. <rails-mailing-list@andreas-
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

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?

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!