【问题标题】:Delete other user except current_user in rails app using devise gem使用devise gem在rails应用程序中删除除current_user之外的其他用户
【发布时间】:2017-02-09 14:22:22
【问题描述】:

我正在尝试列出所有正在使用我的应用程序的用户并只删除我想要的那些用户。但是每次我尝试删除我的用户时,它都会传递localhost:3000/users.2 而不是localhost:3000/users/2

而且它也不会删除选定的用户,而是删除当前用户。但我不想删除当前用户。

我没有修改任何控制器或模型,只是在我的视图中使用以下代码来列出和删除用户。

<table class="table table-hover table-striped">
      <thead>
      <tr class="bg-primary">
        <th>S.N.</th>
        <th>Name</th>
        <th>Email</th>
        <th>Created at</th>
        <th>Action</th>
      </tr>
      </thead>
      <% @users.each_with_index do |u, i| %>
          <% if current_user.email != u.email %>
              <tbody>
              <tr>
                <td><%= (@page.to_i - 1) * @perpage + i+1 %></td>
                <td>
                  <% if u.first_name.present?%>
                      <%= u.first_name.slice(0, 1).capitalize + u.first_name.slice(1..-1) %>
                  <% end %>
                  <% if u.middle_name.present? %>
                      <%= u.middle_name.slice(0, 1).capitalize + u.middle_name.slice(1..-1) %>
                  <% end %>
                  <% if u.last_name.present? %>
                      <%= u.last_name.slice(0, 1).capitalize + u.last_name.slice(1..-1) %>
                  <% end %>
                </td>
                <td>
                  <%= u.email %>
                </td>
                <td>
                  <%= u.created_at %>
                </td>
                <td>
                  <a data-confirm="Are you sure to delete" rel="nofollow" data-method="delete" href="<%= user_registration_path(u)%>">
                    <span class="fa-delete">
                      <i class="fa fa-trash fa-th-large fa-2x"></i>
                    </span>
                  </a>
                </td>
              </tr>
              </tbody>
          <% end %>
      <% end %>
    </table>

当我尝试删除除 current_user 之外的其他用户时出错

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 devise ruby-on-rails-5


【解决方案1】:

只需使用&lt;% unless u == current_user %&gt; 检查当前用户以获取删除链接

您可以向 current_user 显示其他数据

<% unless u == current_user %>
  <a data-confirm="Are you sure to delete" rel="nofollow" data-method="delete" href="<%= user_registration_path(u)%>">
    <span class="fa-delete">
      <i class="fa fa-trash fa-th-large fa-2x"></i>
    </span>
  </a>
<% end %>

如果您不想显示任何数据

替换

<% if current_user.email != u.email %>

<% unless current_user == u %>

【讨论】:

  • 这不是问题...问题是我只想删除除 current_user 之外的其他用户,但它没有发生...当我尝试删除其他列出的用户时,它会删除当前用户并显示错误message....错误信息在上图中给出......检查一下
【解决方案2】:

如果你只是尝试删除一个用户,我想你已经有了它的逻辑,只需将 a 标记更改为 link_to 辅助方法:

<% unless u == current_user %>
 <%= link_to user_registration_path(u), method: :delete, confirm: "Are you sure to delete?", rel: "nofollow" do %>
   <span class="fa-delete">
     <i class="fa fa-trash fa-th-large fa-2x"></i>
   </span>
 <% end %>
<% end %>

这种方式更干净,更有条理

【讨论】:

    【解决方案3】:

    您尝试删除用户的路由是 Devise 自带的,仅用于删除当前用户。 要删除任何用户,您必须创建一个具有破坏操作的新控制器。之后,将此控制器/操作添加到您的路由中,然后您将能够使用新路由删除您想要的任何用户。

    示例控制器:

    class UsersController < ApplicationController
       def destroy
        user = User.find(params[:id])
        if current_user != user
         if user.destroy
           flash[:success] = "Successfully deleted"
         else
           flash[:error] = "Error"
         end
        end
        redirect_to your_users_path
       end
    end
    

    在您的路线中:

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

    然后在创建链接时:

    <%= link_to "Destroy", admin_destroy_user_path(user), method: :delete, data: { confirm: "You sure?" } %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-21
      • 2016-01-23
      • 2023-03-07
      • 1970-01-01
      • 2014-12-01
      • 2014-01-07
      • 1970-01-01
      相关资源
      最近更新 更多