Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
blog
Project
Project
Details
Activity
Releases
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mis-21-2018-intersession
rails
blog
Commits
aaa3f2bd
Commit
aaa3f2bd
authored
Jul 02, 2018
by
John Noel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimization #2 - Controller Callbacks
parent
8c65cdc8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
11 deletions
+8
-11
posts_controller.rb
app/controllers/posts_controller.rb
+8
-11
No files found.
app/controllers/posts_controller.rb
View file @
aaa3f2bd
class
PostsController
<
ApplicationController
class
PostsController
<
ApplicationController
before_action
:set_post
,
only:
[
:show
,
:destroy
,
:edit
,
:update
]
def
index
def
index
@posts
=
Post
.
all
@posts
=
Post
.
all
end
end
def
show
def
show
@post
=
Post
.
find_by
(
id:
params
[
:id
])
end
end
def
new
def
new
...
@@ -22,22 +22,14 @@ class PostsController < ApplicationController
...
@@ -22,22 +22,14 @@ class PostsController < ApplicationController
end
end
def
destroy
def
destroy
@post
=
Post
.
find_by
(
id:
params
[
:id
])
@post
.
destroy
if
@post
.
present?
redirect_to
posts_path
,
alert:
"You have successfully deleted a post."
@post
.
destroy
alert
=
"You have successfully deleted a post."
else
alert
=
"Post not found."
end
redirect_to
posts_path
,
alert:
alert
end
end
def
edit
def
edit
@post
=
Post
.
find_by
(
id:
params
[
:id
])
end
end
def
update
def
update
@post
=
Post
.
find_by
(
id:
params
[
:id
])
if
@post
.
update
(
post_params
)
if
@post
.
update
(
post_params
)
redirect_to
posts_path
,
notice:
"You have successfully updated a post."
redirect_to
posts_path
,
notice:
"You have successfully updated a post."
else
else
...
@@ -49,4 +41,9 @@ class PostsController < ApplicationController
...
@@ -49,4 +41,9 @@ class PostsController < ApplicationController
def
post_params
def
post_params
params
.
require
(
:post
).
permit!
params
.
require
(
:post
).
permit!
end
end
def
set_post
@post
=
Post
.
find_by
(
id:
params
[
:id
])
redirect_to
posts_path
,
alert:
"Post not found"
if
@post
.
nil?
end
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