[英]Resources delete method not working with Devise
I am using devise gem for authentication. 我正在使用devise gem进行身份验证。 For the admin user I am trying to create a page where he will see the list of all users and will be able to delete them.
对于管理员用户,我正在尝试创建一个页面,在该页面上,他将看到所有用户的列表,并将其删除。 Because devise does not provide action for delete I created a controller where I created destroy action.
由于devise不提供删除操作,因此我在创建destroy动作的地方创建了一个控制器。
controller: 控制器:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to("/devise")
end
The view is simple, it just lists all users and posts a delete link next to them. 该视图很简单,它仅列出所有用户并在其旁边发布删除链接。
<% @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 %>
Up until this point it works - the page displays all users and the delete link too. 到目前为止,它仍然有效-该页面还显示所有用户以及删除链接。 However, when I try to delete random users it just opens them in new window instead of deleting them.
但是,当我尝试删除随机用户时,它只是在新窗口中打开它们而不是删除它们。
My routes are as follows: 我的路线如下:
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
I need to make the delete function work. 我需要使删除功能起作用。 Thank you.
谢谢。
This is the solution for the question I posted: 这是我发布的问题的解决方案:
controlled: 受控:
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to("/users")
end
view: 视图:
<%= link_to "delete", user, method: :delete,
data: { confirm: "Are you sure you want to permanently delete this user?" } %>
route: 路线:
match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete
As far as i know, device doesn't handle destroy action. 据我所知,设备无法处理破坏动作。 You should create a custom controller and provide a destroy action.
您应该创建一个自定义控制器并提供销毁操作。 In my case, i create a users_controller
就我而言,我创建了一个users_controller
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: html.erb:
<%= link_to 'Delete', user, method: :delete, data: { confirm: 'Are you sure?' } %>
routes.rb: routes.rb中:
match '/users/:id', :to => 'users#destroy', :as => :destroy_user, :via => :delete
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.