删除设计生成模型中的用户 [英] deleting users in devise generated model
问题描述
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屋!