无法从 Rails 中的管理控制器中删除用户
Can't delete user from admin controller in Rails
我有使用 Devise gem 的用户模型。我正在尝试通过管理部分管理用户,我得到以下信息:
- 我可以创建一个新用户。
- 我可以编辑现有用户。
但我无法删除用户。
按下销毁按钮后,我被重定向到用户信息页面。
enter image description here
控制器:
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
路由选择
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
查看部分
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
我想通了:
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.
def destroy
user = User.find(params[:id])
user.destroy
redirect_to admin_users_path
end
试试这个
我找到了问题的答案。我用的是rails7,它用的是turbo,不是ujs,所以需要加一个方法才能正确删除:
data: { "turbo-method": :delete }
<%= link_to t('navigation.sign_out'), destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>
这就是你所需要的。
我有使用 Devise gem 的用户模型。我正在尝试通过管理部分管理用户,我得到以下信息:
- 我可以创建一个新用户。
- 我可以编辑现有用户。 但我无法删除用户。
按下销毁按钮后,我被重定向到用户信息页面。 enter image description here
控制器:
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
路由选择
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
查看部分
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
我想通了:
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.
def destroy
user = User.find(params[:id])
user.destroy
redirect_to admin_users_path
end
试试这个
我找到了问题的答案。我用的是rails7,它用的是turbo,不是ujs,所以需要加一个方法才能正确删除:
data: { "turbo-method": :delete }
<%= link_to t('navigation.sign_out'), destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>
这就是你所需要的。