我正在使用devise gem进行身份验证。对于管理员用户,我正在尝试创建一个页面,在该页面中,他将看到所有用户的列表,并且可以删除它们。因为devise不提供删除操作,所以我创建了一个控制器,我在其中创建了destroy动作。
控制器:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to("/devise")
end
视图很简单,它只列出所有用户并在其旁边发布删除链接。
<% @users.each do |user| %>
<%= user.email %> <%= link_to 'Delete', :controller => :devise, :action => :destroy, :id => (user[:id]), method: :delete, data: { confirm: "Are you sure you want to delete this user permanently?" } %>
<% end %>
直到此时它起作用 - 该页面也显示所有用户和删除链接。但是,当我尝试删除随机用户时,只需在新窗口中打开它们,而不是删除它们。
我的路线如下:
Rails.application.routes.draw do
devise_for :admins
devise_for :users
resources :devise
resources :centres
resources :users
#match '/users/:id', :to => 'devise#show', :as => :user, :via => :get
match '/users/:id', :to => 'devise#destroy', :as => :destroy_user, :via => :delete
root 'welcome#index'
get '/index', to: 'welcome#index'
get '/about', to: 'welcome#about'
get '/help', to: 'welcome#help'
end
我需要让删除功能起作用。谢谢。
答案 0 :(得分:1)
这是我发布的问题的解决方案:
控制:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to("/users")
end
视图:
<%= link_to "delete", user, method: :delete,
data: { confirm: "Are you sure you want to permanently delete this user?" } %>
路线:
match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete
答案 1 :(得分:0)
据我所知,设备不会处理破坏行为。您应该创建一个自定义控制器并提供销毁操作。就我而言,我创建了一个users_controller
users_controller:
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :ok }
end
end
html.erb:
<%= link_to 'Delete', user, method: :delete, data: { confirm: 'Are you sure?' } %>
routes.rb中:
match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete