1 00:00:00,000 --> 00:00:04,638 [MUSIC] 2 00:00:04,638 --> 00:00:08,640 Working with associated models in your controllers won't be much different from 3 00:00:08,640 --> 00:00:10,619 working with them in the Rails console. 4 00:00:10,619 --> 00:00:12,321 But rendering them in your views and 5 00:00:12,321 --> 00:00:16,340 representing the association in your routes might take a little more work. 6 00:00:16,340 --> 00:00:19,560 We'll cover those details and more in this stage. 7 00:00:19,560 --> 00:00:24,340 We can associate comments with a post in the Rails console, but users of our blog 8 00:00:24,340 --> 00:00:28,780 app can't view a post's comments from their browser yet, let's fix that. 9 00:00:28,780 --> 00:00:32,305 We can use all the same association methods we were using in the Rails 10 00:00:32,305 --> 00:00:35,120 console, within controllers and view as well. 11 00:00:35,120 --> 00:00:38,660 That's going to be the key to allowing create, read, update, and 12 00:00:38,660 --> 00:00:42,740 delete actions on our associated models from a user's web browser. 13 00:00:42,740 --> 00:00:46,620 First, let's try applying that to viewing a list of the comments associated with 14 00:00:46,620 --> 00:00:48,180 a post. 15 00:00:48,180 --> 00:00:52,230 So let's go into the show view for our posts. 16 00:00:52,230 --> 00:00:54,160 And on an individual post page, 17 00:00:54,160 --> 00:00:58,240 we're going to add a div that shows all the comments for that post. 18 00:00:58,240 --> 00:01:03,875 So we'll add a new div HTML element here, and we're gonna give it an ID of comments. 19 00:01:03,875 --> 00:01:06,060 We don't have any particular use for that ID right now. 20 00:01:06,060 --> 00:01:08,990 It'll just be useful for identifying this section later. 21 00:01:10,120 --> 00:01:14,634 Let's put a level 1 heading in here of Comments. 22 00:01:14,634 --> 00:01:19,368 And let's start by looping through all the comments on our post, 23 00:01:19,368 --> 00:01:23,170 which will be in the @post instance variable. 24 00:01:23,170 --> 00:01:27,640 We can access all the comments with the comments method on that post. 25 00:01:27,640 --> 00:01:31,497 And because comments returns an array-like collection, 26 00:01:31,497 --> 00:01:36,442 we can call the each method on it to process each comment in that collection. 27 00:01:36,442 --> 00:01:43,940 So we'll provide a block here, and that block will take a parameter of comment. 28 00:01:46,648 --> 00:01:50,530 Then let's add the end keyword to end our each loop. 29 00:01:50,530 --> 00:01:53,566 The contents of this loop are going to be repeated for 30 00:01:53,566 --> 00:01:58,191 each element in the comments collection, or no times if there are no comments. 31 00:01:58,191 --> 00:02:00,840 So for each comment, we're going to create a div element. 32 00:02:04,592 --> 00:02:08,170 And in that div element, we're going to include the commenter name. 33 00:02:08,170 --> 00:02:12,973 We'll put that in a strong tag so that it gets highlighted. 34 00:02:12,973 --> 00:02:18,648 And then we'll say, comment.name 35 00:02:18,648 --> 00:02:24,070 says, And close the strong tag. 36 00:02:24,070 --> 00:02:29,634 Then we'll put the actual content of the comment down here, comment.content. 37 00:02:31,452 --> 00:02:36,421 We'll save that, and let's visit our list of posts after starting 38 00:02:36,421 --> 00:02:39,570 our Rails server up, bin/rails server. 39 00:02:41,930 --> 00:02:43,818 Reload that and visit the first post, 40 00:02:43,818 --> 00:02:46,760 which is what we've been putting the comments on. 41 00:02:46,760 --> 00:02:50,992 Show that, and there's our list of comments. 42 00:02:50,992 --> 00:02:54,568 The view looped through each comment in the collection and created a div for 43 00:02:54,568 --> 00:02:55,910 each one of them. 44 00:02:55,910 --> 00:02:58,790 So now you've got a working list of comments for each post. 45 00:02:58,790 --> 00:03:02,050 This works okay for an associated model with only two attributes, 46 00:03:02,050 --> 00:03:03,310 like comments have. 47 00:03:03,310 --> 00:03:06,400 But once you start working with more complex associated models, 48 00:03:06,400 --> 00:03:10,710 this is gonna get unwieldy to embed within the post view. 49 00:03:10,710 --> 00:03:14,660 Up next we'll look at a better way to split up the ERB code using partials.