Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sample_app
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Đường Sỹ Hoàng
sample_app
Commits
9495aaca
Commit
9495aaca
authored
Nov 19, 2019
by
Đường Sỹ Hoàng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finish user edit, update, index, and destroy actions
parent
8a67a6cf
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
46 additions
and
15 deletions
+46
-15
app/controllers/users_controller.rb
+12
-1
app/views/users/_user.html.erb
+8
-0
app/views/users/index.html.erb
+4
-7
db/migrate/20191115085103_add_admin_to_users.rb
+5
-0
db/schema.rb
+2
-1
db/seeds.rb
+2
-6
test/controllers/users_controller_test.rb
+13
-0
No files found.
app/controllers/users_controller.rb
View file @
9495aaca
class
UsersController
<
ApplicationController
before_action
:logged_in_user
,
only:
[
:index
,
:edit
,
:update
]
before_action
:logged_in_user
,
only:
[
:index
,
:edit
,
:update
,
:destroy
]
before_action
:correct_user
,
only:
[
:edit
,
:update
]
before_action
:admin_user
,
only: :destroy
def
index
@users
=
User
.
paginate
(
page:
params
[
:page
])
end
...
...
@@ -38,8 +39,18 @@ class UsersController < ApplicationController
end
end
def
destroy
User
.
find
(
params
[
:id
]).
destroy
flash
[
:success
]
=
"User deleted"
redirect_to
users_url
end
private
def
admin_user
redirect_to
(
root_url
)
unless
current_user
.
admin?
end
def
user_params
params
.
require
(
:user
).
permit
(
:name
,
:email
,
:password
,
:password_confirmation
)
end
...
...
app/views/users/_user.html.erb
0 → 100644
View file @
9495aaca
<li>
<%=
gravatar_for
user
,
size:
50
%>
<%=
link_to
user
.
name
,
user
%>
<%
if
current_user
.
admin?
&&
!
current_user?
(
user
)
%>
|
<%=
link_to
"delete"
,
user
,
method: :delete
,
data:
{
confirm:
"You sure?"
}
%>
<%
end
%>
</li>
\ No newline at end of file
app/views/users/index.html.erb
View file @
9495aaca
<%
provide
(
:title
,
"All users"
)
%>
<%
provide
(
:title
,
'All users'
)
%>
<h1>
All users
</h1>
<%=
will_paginate
%>
<ul
class=
"users"
>
<%
@users
.
each
do
|
user
|
%>
<li>
<%=
gravatar_for
user
,
size:
50
%>
<%=
link_to
user
.
name
,
user
%>
</li>
<%=
render
@users
%>
<%
end
%>
</ul>
<%
will_paginate
%>
\ No newline at end of file
<%=
will_paginate
%>
\ No newline at end of file
db/migrate/20191115085103_add_admin_to_users.rb
0 → 100644
View file @
9495aaca
class
AddAdminToUsers
<
ActiveRecord
::
Migration
[
5.1
]
def
change
add_column
:users
,
:admin
,
:boolean
,
default:
false
end
end
db/schema.rb
View file @
9495aaca
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2019111
3080850
)
do
ActiveRecord
::
Schema
.
define
(
version:
2019111
5085103
)
do
create_table
"users"
,
force: :cascade
do
|
t
|
t
.
string
"name"
...
...
@@ -19,6 +19,7 @@ ActiveRecord::Schema.define(version: 20191113080850) do
t
.
datetime
"updated_at"
,
null:
false
t
.
string
"password_digest"
t
.
string
"remember_digest"
t
.
boolean
"admin"
t
.
index
[
"email"
],
name:
"index_users_on_email"
,
unique:
true
end
...
...
db/seeds.rb
View file @
9495aaca
User
.
create!
(
name:
"Example User"
,
email:
"example@railstutorial.org"
,
password
:"foobar"
,
password_confirmation:
"foobar"
)
User
.
create!
(
name:
"Example User"
,
email:
"example@railstutorial.org"
,
password
:"foobar"
,
password_confirmation:
"foobar"
,
admin:
true
)
99
.
times
do
|
n
|
name
=
Faker
::
Name
.
name
email
=
"example-
#{
n
+
1
}
@railstutorial.org"
password
=
"password"
User
.
create!
(
name:
name
,
email:
email
,
password
:password
,
password_confirmation:
password
)
User
.
create!
(
name:
name
,
email:
email
,
password
:password
,
password_confirmation:
password
)
end
test/controllers/users_controller_test.rb
View file @
9495aaca
...
...
@@ -29,4 +29,17 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert
flash
.
empty?
assert_redirected_to
root_url
end
test
"should redirect update when not logged in"
do
patch
user_path
(
@user
),
params:
{
user:
{
name:
@user
.
name
,
email:
@user
.
email
}
}
assert_not
flash
.
empty?
assert_redirected_to
login_url
end
test
"should not allow the admin attribute to be edited via the web"
do
log_in_as
(
@other_user
)
assert_not
@other_user
.
admin?
patch
user_path
(
@other_user
),
params:
{
user
:{
password:
@other_user
.
password
,
password_confirmation:
@other_user
.
password
,
admin:
true
}
}
assert_not
@other_user
.
reload
.
admin?
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment