17

For my application, I have user accounts that people can sign up. I use the devise gem for the setup.

I also have a users page that lists out all the users registered to the site along with a destroy link. I want my administrator to be able to delete users and have it redirected to this users listing page. But when I click the link to destroy a specific user, it just redirects to the user profile page and does not delete the user from the database.

Does somebody know why?

**UPDATE: Updated code below as recommended and works now.

users_controller.rb

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    if @user.destroy
        redirect_to root_url, notice: "User deleted."
    end
  end

users/index.html.erb

<div class = "container">
    <div id="usersview">
    <b>USERS DIRECTORY</b>
    <%= render @users %>
</div>

<center>
    <%= will_paginate @users %>
</center>
</div>

users/_user.html.erb

<div class="comments">
  <%= link_to user.name, user %>
  <% if current_user.try(:admin?) && !current_user?(user) %>
       <%= link_to "Destroy", admin_destroy_user_path(user), method: :delete, data: { confirm: "You sure?" } %>
  <% end %>
</div>

routes.rb

devise_for :users    
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
match 'users/:id' => 'users#show', as: :user
resources :users
spl
  • 609
  • 2
  • 9
  • 20

5 Answers5

15

Devise doesn't provide this functionality out of the box. You have created your own destroy action, but you also have to create a custom route to that action.

In your routes:

 match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user

And then when creating the link:

<%= link_to "Destroy", admin_destroy_user_path(user), method: :delete, data: { confirm: "You sure?" } %>
AlexBrand
  • 11,971
  • 20
  • 87
  • 132
  • I updated (see question edited) as you noted but same results. Not destroying and routes to show page. Is it possible my routes are not in the right order? – spl Apr 29 '13 at 22:53
  • try placing the custom route above the `devise_for` call. – AlexBrand Apr 29 '13 at 22:54
  • I don't think you need the `resources :users` – AlexBrand Apr 29 '13 at 23:14
  • I need resources :users for some "member do get:..." but the reorder works now, see updated code in question above. – spl Apr 29 '13 at 23:19
6

According to devise 4.1 rake routes and views, using the following will help you delete the account once logged in as a user.

<%= link_to "Cancel my account", registration_path(current_user), data: { confirm: "Are you sure?" }, method: :delete %>
Stephane Paquet
  • 2,315
  • 27
  • 31
4

I think you have in your routes.rb: resources :users

The problem is that you are not passing the user that you want to delete when clicking:

users/_user.html.erb

<%= link_to "Destroy", user, method: :delete, data: { confirm: "You sure?" } %>

See that in second param I added user instead of user_url.

users_controller.rb

 def destroy
    @user = User.find(params[:id])

    if @user.destroy
        redirect_to root_url, notice: "User deleted."
    end
  end

In controller I removed an @user.destroy. You were calling it twice.

Hope it helps!

cortex
  • 5,036
  • 3
  • 31
  • 41
2

At least the latest version of Devise does provide such functionality out of the box, if a Devise-backed model has :registerable included.

class User < ApplicationRecord
  devise :database_authenticatable, :registerable
end

Somewhere in app/views/layouts/application.html.erb:

<%= link_to 'Delete my account', user_registration_path, method: :delete if user_signed_in? %>
Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
0

Or from rails c you can do something like this where "x" is user id.

user = User.where(id: x)
user.destroy(1) 
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48