Skip to content

Commit a8305b3

Browse files
authored
Merge pull request chi-otters-2017#39 from chi-otters-2017/admin-delete-users
Admin delete users
2 parents 8f2fd50 + b637e82 commit a8305b3

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

app/controllers/users_controller.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ class UsersController < ApplicationController
55
def index
66
if admin?
77
@users = User.where(is_ken: false)
8+
else
9+
flash[:not_admin] = "You do not have permission to view this page."
10+
end
11+
end
12+
13+
def show
14+
if admin?
15+
@user = User.find_by(id: friend_id)
16+
else
17+
@user = User.find_by(id: session[:user_id])
818
end
919
end
1020

@@ -25,11 +35,15 @@ def create
2535
end
2636
end
2737

28-
def show
38+
def destroy
2939
if admin?
30-
@user = User.find_by(id: friend_id)
40+
@user = User.find(params[:id])
41+
@user.destroy
42+
43+
redirect_to users_path
3144
else
32-
@user = User.find_by(id: session[:user_id])
45+
flash[:not_admin] = "You do not have permission to view this page."
46+
redirect_to "/"
3347
end
3448
end
3549

app/views/categories/index.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<h1 class='title'>Welcome to Chez Ken</h1>
22
<h3><%= flash[:notice] %></h3>
3+
<h3><%= flash[:not_admin] %></h3>
34

45
<div class='container'>
56
<div class='container col-4-md wd-17-m mg-0'>

app/views/users/index.html.erb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
<h3 class='notice'><%= flash[:notice] %><h3>
2-
1+
<% if admin? %>
32

43
<h1>Friends of Ken</h1>
5-
64
<h3>Select a friend to view their profile</h3>
75

8-
<ul>
9-
<% @users.each do |user| %>
10-
<li><%= link_to "#{user.name}", profile_path(user) %></li>
11-
<% end %>
12-
</ul>
6+
<table>
7+
<% @users.each do |user| %>
8+
<tr>
9+
<td><%= link_to "#{user.name}", profile_path(user) %></td>
10+
<td><%= link_to "Delete", user_path(user),
11+
method: :delete,
12+
data: { confirm: 'Are you sure?' } %></td>
13+
</tr>
14+
<% end %>
15+
</table>
16+
17+
18+
<% else %>
19+
<h3 class='notice'><%= flash[:not_admin] %><h3>
20+
<% end%>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
resources :users, only: :index
1414

15+
resources :users, only: [:destroy]
16+
1517
resources :categories do
1618
resources :recipes, except: [:index, :show]
1719
end

0 commit comments

Comments
 (0)