删除设计生成模型中的用户 [英] deleting users in devise generated model

查看:117
本文介绍了删除设计生成模型中的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我是新的轨道。我使用devise gem进行用户身份验证。现在我想给管理员一种删除其他用户的方式。用户的ID不会传递到我的销毁操作。这是我的代码



user_controller.rb

  class UsersController< ApplicationController 

def destroy
User.find(params [:id])。destroy
redirect_to dashboard_path
end

end

dashboard.html.erb

 code><%if current_user.admin == true%> 
<%@users = User.all%>
<%@ users.each do | user | %GT;
<%= user.email%>
| <%= link_todelete,destroy_path,method::delete,data:{confirm:你确定}%>< br>
<%end%>
<%end%>


解决方案

首先,您不应该分配实例变量直接在你的意见。这是控制器的责任。所以,在上面的例子中,正确的做法是这样的:

 #users_controller.rb 

class UsersController< ApplicationController
def dashboard
@users = User.all
end

def destroy
User.find(params [:id])。destroy
redirect_to dashboard_path
end
end

您的视图应该看起来像这个:

 #dashboard.html.erb 

<%如果current_user.admin == true %GT;
<%@ users.each do | user | %GT;
<%= user.email%>
| <%= link_todelete,user,method::delete,data:{confirm:你确定}%>< br>
<%end%>
<%end%>

最后但并非最不重要的= P确保你的routes.rb有这样的东西:

 #routes.rb 

删除/ users /:id=> users#destroy,as::user

当然这只是一个基于你的问题的例子,但它应该像charm = P


一样工作

First of all I am new to rails. I used devise gem for user authentication. Now I want to provide admin a way to delete other users. id of the user is not passing to my destroy action. Here is my code

user_controller.rb

  class UsersController < ApplicationController

  def destroy
    User.find(params[:id]).destroy
    redirect_to dashboard_path
  end

end

dashboard.html.erb

<% if current_user.admin == true %>
  <% @users = User.all %>
  <% @users.each do |user| %>
    <%= user.email %>
    | <%= link_to "delete", destroy_path, method: :delete, data: { confirm: "Are you sure"} %><br>
  <% end %>
<% end %>

解决方案

First of all, You shouldn't assign instance variables directly in your views. This is a Controller responsibility. So, in the above example, the right thing to do is something like this:

# users_controller.rb

class UsersController < ApplicationController
  def dashboard
    @users = User.all
  end

  def destroy
    User.find(params[:id]).destroy
    redirect_to dashboard_path
  end
end

And your view should look something like this:

# dashboard.html.erb

<% if current_user.admin == true %>
   <% @users.each do |user| %>
     <%= user.email %>
     | <%= link_to "delete", user, method: :delete, data: { confirm: "Are you sure"} %><br>
  <% end %>
<% end %>

And last but not least =P make sure your routes.rb have something like this:

# routes.rb

delete "/users/:id" => "users#destroy", as: :user

Of course it's just an example based on your question, but it should work like a charm =P

这篇关于删除设计生成模型中的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆