Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We've added a way to create comments that belong to a post. Now let's add a means of updating those comments.
We've added a way to create comments that belong to a post. Now let's add a means of updating those comments.
- We want an "edit" link to appear next to each comment.
- We'll get that by editing the
_comment
partial.
views/comments/_comment.html.erb
<div class="comment-admin">
<%= link_to "Edit", edit_post_comment_path(@post, comment) %>
</div>
- Next we need an
edit
action on theCommentsController
:
controllers/comments_controller.rb
def edit
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
end
-
@post = Post.find(params[:post_id])
code is repeated betweencreate
andedit
actions, and we'll need@post
for other actions - Use
before_action
to set@post
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.create(comment_params)
redirect_to @post
end
def edit
@comment = Comment.find(params[:id])
end
private
def comment_params
params.require(:comment).permit(:name, :content)
end
def set_post
@post = Post.find(params[:post_id])
end
end
- When we click the "Edit" link, it calls our
edit
action, but there's no view. So let's create one:
views/comments/edit.html.erb
<h1>Edit Comment</h1>
<%= render partial: "form", locals: {comment: @comment} %>
- We need an
update
action onCommentsController
so we can submit the form:
def update
@comment = Comment.find(params[:id])
@comment.update(comment_params)
redirect_to @post # @post will be set by before_action
end
We've added a way to create
comments that belong to a post.
0:00
Now let's add a means of
updating those comments.
0:03
We're going to want an edit link to
appear next to each comment in the list.
0:06
We'll get that by editing
the comment partial.
0:10
So we'll go here to our views directory
> comments sub-directory, and
0:13
we'll edit the comment partial.
0:17
Here within the main comment div
we're going to add a new div, and
0:19
we'll give it a class of comment admin.
0:24
Should be useful later,
we're not going to use it right now.
0:28
Close out the div.
0:33
And now to set up our link.
0:36
We'll insert an erb tag,
with a call to the link_to method.
0:37
We're going to want the link text to read
Edit, as in edit the current comment.
0:43
And now we need to provide the path
that the link is going to point to.
0:48
We're going to call
edit_post_comment_path.
0:51
And this call is going to
need multiple parameters.
0:58
First, it's going to need a post to
provide the ID used in the post ID URL
1:01
parameter.
1:05
So we'll get that by providing the post
object that's in the post instance
1:06
variable.
1:10
Then it's going to need a common
ID that it's going to edit.
1:12
You can get that by providing the common
metal object that's in the common
1:15
local variable.
1:20
Close out the erb tag and save that, and
1:23
let's refresh our browser and
there are our edit links.
1:26
But if we click on it,
1:31
we'll see the action edit could not
be found on the CommentsController.
1:32
So we're gonna need to go to our
comment controller and add that action.
1:37
So we'll define and edit method.
1:45
And again, we're going to need
the post that the comment belongs to.
1:51
So we'll assign to
the post instance variable
1:54
the result of calling
Post.find with the parameter
1:59
post_id, which it'll get from the URL.
2:05
Then we're also going to need
the comment that we're working with,
2:11
which will assign to
the comment instance variable.
2:14
And that will be the result, we'll call
it comment.find with the ID parameter.
2:17
Let's save this and try reloading.
2:25
Visit the edit link again.
2:30
And it looks like it's getting
to the edit action successfully.
2:33
We don't have a view to render for
this action right now,
2:36
but we'll take care of that momentarily.
2:38
There's another issue I
want to address first.
2:41
Notice that this post =
Post.find(params[post_id]) code is
2:44
repeated between both the create and
edit methods.
2:48
And we're going to need our post instance
variable set for other actions, too.
2:52
Let's go ahead and
cut this code out of there and
2:56
add a private method named set post.
3:00
Paste in there.
3:06
Then we can remove that code
from our edit method as well.
3:11
And we can set set_post up to be called
as a before_action on the controller.
3:17
Now before both our create and
edit actions run this set_post method will
3:29
be called and
the post instance variable will be set.
3:34
Let's save that now let's get
back to our original problem.
3:40
Only click on the edit link,
it calls our edit action but
3:43
there's no view for it to render.
3:47
So let's go and create that now.
3:50
We'll go into the views
comments controller and
3:52
we're going to create a new file,
named edit.html.erb.
3:56
We'll add a heading saying
what we're doing on this page.
4:01
We're editing a comment.
4:03
And because we already
have our form partial,
4:08
all we have to do is call
render with that partial.
4:10
So we'll say render partial form and
4:14
all we have to do is say form.
4:19
We don't have to specify what
directory it's contained.
4:22
Since it's in the same
directory as this edit few.
4:26
And then we need to set up
the comment local variable.
4:29
So we'll say Locals Comment and
we're going to set
4:32
that equal to our comments
instance variable.
4:38
Save that and
let's go back to our post view and
4:42
try editing a comment again.
4:47
And this time the edit template is called
which in turn calls the form partial.
4:51
So let's make a little change to this,
we will add a little more emphasis on
4:56
the comment content, and
now let's try submitting the form.
5:00
We get the error that there's no update
action on our comments controller, so
5:04
let's add that now.
5:08
We'll add a method named update.
5:13
And first, we're going to need to find
the comment that we're working with.
5:18
So we'll assign to
the comment instance variable
5:21
the result of calling
Comment.find the id parameter.
5:27
Then we need to update this comment
with our comment parameters.
5:37
So we'll say comment.update
comment_params.
5:40
And as it does when we're creating a new
comment, it'll call the comment params
5:48
method and use those parameters to
update our existing comment object.
5:53
Finally, we're going to need to
show our updated comment object,
5:57
which we're going to want to
do in the context of a post.
6:01
So we'll say redirect_to @post.
6:03
Remember that we set up
the set_post before action, so
6:10
that's this @post instance variable.
6:14
Save that, and
let's try editing that comment again.
6:16
Go here, we'll add a little more emphasis
and we'll hit Update Comment and
6:22
you can see our edited comment
here in the list on the post page.
6:27
So now we've got updating of
a comment working as well.
6:32
The last thing we'll need is the ability
to delete comments from a post.
6:35
We'll look at that next.
6:39
You need to sign up for Treehouse in order to download course files.
Sign up