1 00:00:00,220 --> 00:00:03,209 We've added a way to create comments that belong to a post. 2 00:00:03,209 --> 00:00:06,780 Now let's add a means of updating those comments. 3 00:00:06,780 --> 00:00:10,558 We're going to want an edit link to appear next to each comment in the list. 4 00:00:10,558 --> 00:00:13,730 We'll get that by editing the comment partial. 5 00:00:13,730 --> 00:00:17,647 So we'll go here to our views directory > comments sub-directory, and 6 00:00:17,647 --> 00:00:19,490 we'll edit the comment partial. 7 00:00:19,490 --> 00:00:24,457 Here within the main comment div we're going to add a new div, and 8 00:00:24,457 --> 00:00:27,540 we'll give it a class of comment admin. 9 00:00:28,960 --> 00:00:33,655 Should be useful later, we're not going to use it right now. 10 00:00:33,655 --> 00:00:34,790 Close out the div. 11 00:00:36,270 --> 00:00:37,567 And now to set up our link. 12 00:00:37,567 --> 00:00:43,570 We'll insert an erb tag, with a call to the link_to method. 13 00:00:43,570 --> 00:00:48,417 We're going to want the link text to read Edit, as in edit the current comment. 14 00:00:48,417 --> 00:00:51,950 And now we need to provide the path that the link is going to point to. 15 00:00:51,950 --> 00:00:58,142 We're going to call edit_post_comment_path. 16 00:00:58,142 --> 00:01:01,285 And this call is going to need multiple parameters. 17 00:01:01,285 --> 00:01:05,665 First, it's going to need a post to provide the ID used in the post ID URL 18 00:01:05,665 --> 00:01:06,476 parameter. 19 00:01:06,476 --> 00:01:10,949 So we'll get that by providing the post object that's in the post instance 20 00:01:10,949 --> 00:01:12,070 variable. 21 00:01:12,070 --> 00:01:15,310 Then it's going to need a common ID that it's going to edit. 22 00:01:15,310 --> 00:01:20,043 You can get that by providing the common metal object that's in the common 23 00:01:20,043 --> 00:01:21,200 local variable. 24 00:01:23,380 --> 00:01:26,647 Close out the erb tag and save that, and 25 00:01:26,647 --> 00:01:31,411 let's refresh our browser and there are our edit links. 26 00:01:31,411 --> 00:01:32,581 But if we click on it, 27 00:01:32,581 --> 00:01:37,450 we'll see the action edit could not be found on the CommentsController. 28 00:01:37,450 --> 00:01:45,940 So we're gonna need to go to our comment controller and add that action. 29 00:01:45,940 --> 00:01:47,680 So we'll define and edit method. 30 00:01:51,120 --> 00:01:54,244 And again, we're going to need the post that the comment belongs to. 31 00:01:54,244 --> 00:01:59,509 So we'll assign to the post instance variable 32 00:01:59,509 --> 00:02:05,449 the result of calling Post.find with the parameter 33 00:02:05,449 --> 00:02:11,130 post_id, which it'll get from the URL. 34 00:02:11,130 --> 00:02:14,650 Then we're also going to need the comment that we're working with, 35 00:02:14,650 --> 00:02:17,390 which will assign to the comment instance variable. 36 00:02:17,390 --> 00:02:24,180 And that will be the result, we'll call it comment.find with the ID parameter. 37 00:02:25,840 --> 00:02:30,805 Let's save this and try reloading. 38 00:02:30,805 --> 00:02:33,020 Visit the edit link again. 39 00:02:33,020 --> 00:02:36,172 And it looks like it's getting to the edit action successfully. 40 00:02:36,172 --> 00:02:38,786 We don't have a view to render for this action right now, 41 00:02:38,786 --> 00:02:40,750 but we'll take care of that momentarily. 42 00:02:41,820 --> 00:02:44,095 There's another issue I want to address first. 43 00:02:44,095 --> 00:02:48,665 Notice that this post = Post.find(params[post_id]) code is 44 00:02:48,665 --> 00:02:52,630 repeated between both the create and edit methods. 45 00:02:52,630 --> 00:02:56,700 And we're going to need our post instance variable set for other actions, too. 46 00:02:56,700 --> 00:03:00,340 Let's go ahead and cut this code out of there and 47 00:03:00,340 --> 00:03:03,170 add a private method named set post. 48 00:03:06,569 --> 00:03:07,680 Paste in there. 49 00:03:11,375 --> 00:03:14,530 Then we can remove that code from our edit method as well. 50 00:03:17,937 --> 00:03:23,490 And we can set set_post up to be called as a before_action on the controller. 51 00:03:29,191 --> 00:03:34,089 Now before both our create and edit actions run this set_post method will 52 00:03:34,089 --> 00:03:37,650 be called and the post instance variable will be set. 53 00:03:40,210 --> 00:03:43,379 Let's save that now let's get back to our original problem. 54 00:03:43,379 --> 00:03:47,220 Only click on the edit link, it calls our edit action but 55 00:03:47,220 --> 00:03:50,300 there's no view for it to render. 56 00:03:50,300 --> 00:03:52,469 So let's go and create that now. 57 00:03:52,469 --> 00:03:56,515 We'll go into the views comments controller and 58 00:03:56,515 --> 00:04:01,557 we're going to create a new file, named edit.html.erb. 59 00:04:01,557 --> 00:04:03,990 We'll add a heading saying what we're doing on this page. 60 00:04:03,990 --> 00:04:05,850 We're editing a comment. 61 00:04:08,151 --> 00:04:10,820 And because we already have our form partial, 62 00:04:10,820 --> 00:04:14,480 all we have to do is call render with that partial. 63 00:04:14,480 --> 00:04:19,273 So we'll say render partial form and 64 00:04:19,273 --> 00:04:22,987 all we have to do is say form. 65 00:04:22,987 --> 00:04:26,020 We don't have to specify what directory it's contained. 66 00:04:26,020 --> 00:04:29,493 Since it's in the same directory as this edit few. 67 00:04:29,493 --> 00:04:32,910 And then we need to set up the comment local variable. 68 00:04:32,910 --> 00:04:38,033 So we'll say Locals Comment and we're going to set 69 00:04:38,033 --> 00:04:42,811 that equal to our comments instance variable. 70 00:04:42,811 --> 00:04:47,938 Save that and let's go back to our post view and 71 00:04:47,938 --> 00:04:51,570 try editing a comment again. 72 00:04:51,570 --> 00:04:56,593 And this time the edit template is called which in turn calls the form partial. 73 00:04:56,593 --> 00:05:00,824 So let's make a little change to this, we will add a little more emphasis on 74 00:05:00,824 --> 00:05:04,402 the comment content, and now let's try submitting the form. 75 00:05:04,402 --> 00:05:08,840 We get the error that there's no update action on our comments controller, so 76 00:05:08,840 --> 00:05:10,059 let's add that now. 77 00:05:13,472 --> 00:05:15,500 We'll add a method named update. 78 00:05:18,410 --> 00:05:21,950 And first, we're going to need to find the comment that we're working with. 79 00:05:21,950 --> 00:05:27,130 So we'll assign to the comment instance variable 80 00:05:27,130 --> 00:05:32,939 the result of calling Comment.find the id parameter. 81 00:05:37,051 --> 00:05:40,420 Then we need to update this comment with our comment parameters. 82 00:05:40,420 --> 00:05:48,817 So we'll say comment.update comment_params. 83 00:05:48,817 --> 00:05:53,385 And as it does when we're creating a new comment, it'll call the comment params 84 00:05:53,385 --> 00:05:57,960 method and use those parameters to update our existing comment object. 85 00:05:57,960 --> 00:06:01,209 Finally, we're going to need to show our updated comment object, 86 00:06:01,209 --> 00:06:03,902 which we're going to want to do in the context of a post. 87 00:06:03,902 --> 00:06:10,790 So we'll say redirect_to @post. 88 00:06:10,790 --> 00:06:14,257 Remember that we set up the set_post before action, so 89 00:06:14,257 --> 00:06:16,854 that's this @post instance variable. 90 00:06:16,854 --> 00:06:22,710 Save that, and let's try editing that comment again. 91 00:06:22,710 --> 00:06:27,795 Go here, we'll add a little more emphasis and we'll hit Update Comment and 92 00:06:27,795 --> 00:06:32,018 you can see our edited comment here in the list on the post page. 93 00:06:32,018 --> 00:06:35,440 So now we've got updating of a comment working as well. 94 00:06:35,440 --> 00:06:39,062 The last thing we'll need is the ability to delete comments from a post. 95 00:06:39,062 --> 00:06:40,470 We'll look at that next.