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
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
用户/ 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
用户/ _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
的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
4 个解决方案
#1
10
Devise doesn't provide this functionality out of the box. You have created your own destroy action, but you also have to create a custom route to that action.
Devise不提供开箱即用的功能。您已创建自己的销毁操作,但您还必须创建指向该操作的自定义路径。
In your routes:
在你的路线:
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
And then when creating the link:
然后在创建链接时:
<%= link_to "Destroy", admin_destroy_user_path(user), method: :delete, data: { confirm: "You sure?" } %>
#2
3
I think you have in your routes.rb: resources :users
我认为你有你的routes.rb:resources:users
The problem is that you are not passing the user that you want to delete when clicking:
问题是您在单击时未传递要删除的用户:
users/_user.html.erb
用户/ _user.html.erb
<%= link_to "Destroy", user, method: :delete, data: { confirm: "You sure?" } %>
See that in second param I added user instead of user_url.
在第二个参数中看到我添加了user而不是user_url。
users_controller.rb
users_controller.rb
def destroy
@user = User.find(params[:id])
if @user.destroy
redirect_to root_url, notice: "User deleted."
end
end
In controller I removed an @user.destroy. You were calling it twice.
在控制器中我删除了一个@ user.destroy。你打了两次电话。
Hope it helps!
希望能帮助到你!
#3
3
According to devise 4.1 rake routes and views, using the following will help you delete the account once logged in as a user.
根据设计4.1 rake路线和视图,使用以下内容将帮助您以用户身份登录后删除该帐户。
<%= link_to "Cancel my account", registration_path(current_user), data: { confirm: "Are you sure?" }, method: :delete %>
#4
0
Or from rails c you can do something like this where "x" is user id.
或者从rails c你可以做这样的事情,其中“x”是用户ID。
user = User.where(id: x)
user.destroy(1)
#1
10
Devise doesn't provide this functionality out of the box. You have created your own destroy action, but you also have to create a custom route to that action.
Devise不提供开箱即用的功能。您已创建自己的销毁操作,但您还必须创建指向该操作的自定义路径。
In your routes:
在你的路线:
match 'users/:id' => 'users#destroy', :via => :delete, :as => :admin_destroy_user
And then when creating the link:
然后在创建链接时:
<%= link_to "Destroy", admin_destroy_user_path(user), method: :delete, data: { confirm: "You sure?" } %>
#2
3
I think you have in your routes.rb: resources :users
我认为你有你的routes.rb:resources:users
The problem is that you are not passing the user that you want to delete when clicking:
问题是您在单击时未传递要删除的用户:
users/_user.html.erb
用户/ _user.html.erb
<%= link_to "Destroy", user, method: :delete, data: { confirm: "You sure?" } %>
See that in second param I added user instead of user_url.
在第二个参数中看到我添加了user而不是user_url。
users_controller.rb
users_controller.rb
def destroy
@user = User.find(params[:id])
if @user.destroy
redirect_to root_url, notice: "User deleted."
end
end
In controller I removed an @user.destroy. You were calling it twice.
在控制器中我删除了一个@ user.destroy。你打了两次电话。
Hope it helps!
希望能帮助到你!
#3
3
According to devise 4.1 rake routes and views, using the following will help you delete the account once logged in as a user.
根据设计4.1 rake路线和视图,使用以下内容将帮助您以用户身份登录后删除该帐户。
<%= link_to "Cancel my account", registration_path(current_user), data: { confirm: "Are you sure?" }, method: :delete %>
#4
0
Or from rails c you can do something like this where "x" is user id.
或者从rails c你可以做这样的事情,其中“x”是用户ID。
user = User.where(id: x)
user.destroy(1)