Search code examples
ruby-on-railsdevise

Can't delete user from admin controller in Rails


I have Users model with Devise gem. I'm trying to manage users through the Admin section and I get the following:

  1. I can create a new user.
  2. I can edit an already existing user. but I can't delete the user.

After pressing the Destroy button, I'm redirected to the user information page. enter image description here

Controller:

admin/users_controller.rb

class Admin::UsersController < ApplicationController
  layout "admin_application"

  before_action :set_user, only: [:show, :edit, :update, :destroy]

  def index
    @users = User.where.not(:id => current_user.id)
    #authorize @users
  end

  def show
    @user = User.find(params[:id])
    #authorize @user
  end

  def new
    @user = User.new
    #authorize @user
  end

  def create
    @user = User.new(user_params)
    #authorize @user
    if @user.save
      flash[:notice] = "Successfully created User."
      redirect_to admin_users_path
    else
      render :action => 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
    #authorize @user
  end

  def update
    @user = User.find(params[:id])
    #authorize @user
    params[:user].delete(:password) if params[:user][:password].blank?
    params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?

    if @user.update(user_params)
      flash[:notice] = "Successfully updated User."
      redirect_to admin_users_path
    else
      render :action => 'edit'
    end
  end

  def destroy
    @user.destroy
    redirect_to admin_users_path
    #@user = User.find(params[:id])
    #authorize @user
    #if @user.destroy
    #  flash[:notice] = "Successfully deleted User."
    #  redirect_to admin_users_path
    #else
    #  redirect_to admin_users_path, flash: { error: "User could not be deleted." }
    #end
  end
  private

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

  def user_params
     params.require(:user).permit(:avatar, :first_name, :middle_name, :last_name, :username, :email, :role_id, :password, :password_confirmation)
  end
end

Routing

Rails.application.routes.draw do
  root to: 'dashboard#index'

  # Users routes
  devise_for :users
  match '/users/:id', :to => 'dashboard#index', :as => :user, :via => :get # Show page for current_user

  # Admin routes
  namespace :admin do
    root to: 'dashboard#index'

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

View section

Links in admin/users/show.html.haml

= link_to "Edit", edit_admin_user_path(@user)
= link_to "Delete", admin_user_path(@user), data: { confirm: "Are you sure?" }, method: :delete

I figured out it:

Althow if I try to edit user from Index page (lists all users), I'm redirecting to Show page. enter image description here So I can manage profiles only from Show page, but button Back in browser show Edit page. So it is not important isue.


Solution

  • I found the answer to the question. I'm using rails 7, which uses turbo, not ujs, so I need to add a method to delete correctly:

    data: { "turbo-method": :delete }
    
    <%= link_to t('navigation.sign_out'), destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>
    

    thats all you need.