views:

91

answers:

2

In routes.rb I have

map.resource :user

In one of my templates I want to

link_to 'delete', some_other_user, :method => :post

I was hoping this would generate a url like

/user/#{some_other_user.id}

but instead get

/user.#{user.to_s}

The only solution I've found is to add a new route

map.delete_user 'users/:id', :controller => 'users', :action => 'destroy', :method => :delete

and then use

link_to 'delete', delete_user_url(user), :method => :delete

This seems a bit hacky, is there a cleaner way?

+1  A: 

A singleton resource would normally be used to perform operations on one resource, for example, the current user. If I wanted to manipulate another user, for example from an administrator's perspective, I would

  map.resources :users

If I only wanted to perform the destroy operation on other users I would

  map.resources :users, :only => [:destroy]

Then you would only be able to perform the destroy action.

In this user editing their account and administrator administrating all accounts situation (if I'm assuming correctly) I would create two controllers,

  • AccountsController - allow the current user to edit their account (singleton resource)
  • UsersController - allow administrators to edit all user accounts

Routes would be like so:

  map.resource :account
  map.resources :users

In this way permissions can be separated into normal user and administrator permissions, if required.

Hope this helps

AntRamm
My first attempt was to use map.resource :user and map.resource :users, :only => [:destroy]mapped to the same controller. I didn't think of using separate controllers though, that looks like the way to go.
opsb
A: 

In addition to AntRamm's answer, to get a destroy URL for resources you need to manually specify the method as delete:

link_to 'delete', user_url(some_other_user), :method => :delete

See the docs for more info.

floyd