0

I am creating a form for an admin to go in and list, edit and delete users. I have tried many different variations for deleting a user and nothing works. I am wondering if it is because I need to use devise_for :users and resources :users in my routes.rb. This is because I have uploads/attachments linked to users. But here is my link

<%= link_to 'Delete',user, :method => 'delete',  :confirm => 'Are you sure?' %>

And my routes.rb

# devise_for :users
devise_for :users do
  get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
resources :users  do
  resources :attachments
end

The error I am receiving is The action 'destroy' could not be found for UsersController.

But my users controller has

def destroy
  @user = User.find(params[:id]).destroy
  redirect_to admin_index, :flash => { :success => 'User was successfully deleted.' }
end
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
LABLUEZ
  • 41
  • 1
  • 2
  • This doesn't have anything to do with Devise. Looks similar to [this SO question](http://stackoverflow.com/questions/4606860/rails-3-link-to-to-destroy-not-working). The answer there suggests using a button, which is probably a good idea, but a link works in Rails as well. It might be an issue with your Ajax JavaScript driver. – numbers1311407 Oct 26 '12 at 14:35

1 Answers1

2

If you are not using ajax for the requests, the problem is taht you ar using a link_to

In order to send the :method => 'delete' you have to use a button, jus like this:

<%= button_to 'Delete', user, :method => 'delete',  :confirm => 'Are you sure?' %>

Because destructive action must be performed with a form submission:

http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist

felipeclopes
  • 4,010
  • 2
  • 25
  • 35
  • Please check two things, first, use the rake routes and send me your routes and second just check if you are not closing the controller berfore defining de destroy method. – felipeclopes Oct 27 '12 at 00:35
  • Here is a portion of my users controller with destroy – LABLUEZ Oct 29 '12 at 19:20
  • The controller is closed.My rake routes is really long but for users `users#index POST/users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy` – LABLUEZ Oct 29 '12 at 19:24