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