1 00:00:00,730 --> 00:00:02,870 We're back on the rails log again. 2 00:00:02,870 --> 00:00:05,940 Rails controllers insert load and model data into a view 3 00:00:05,940 --> 00:00:08,060 to create a response for the browser. 4 00:00:08,060 --> 00:00:11,950 The view is responsible for displaying data to your users. 5 00:00:11,950 --> 00:00:16,110 Views usually though not always take the form of HTML templates with Ruby 6 00:00:16,110 --> 00:00:18,120 expressions embedded in them. 7 00:00:18,120 --> 00:00:20,720 Those expressions refer to data loaded from the model 8 00:00:20,720 --> 00:00:23,110 to populate an HTML page with data. 9 00:00:24,830 --> 00:00:27,540 This line in our controller renders the view. 10 00:00:27,540 --> 00:00:28,050 And remember, 11 00:00:28,050 --> 00:00:31,880 even if we remove this line again the same thing would happen by default. 12 00:00:31,880 --> 00:00:37,031 This layout application parameter here instructs 13 00:00:37,031 --> 00:00:43,770 Rails to load App > Views > Layouts > application.html.erb. 14 00:00:43,770 --> 00:00:45,820 This is an ERB template. 15 00:00:45,820 --> 00:00:48,320 ERB stands for embedded ruby. 16 00:00:48,320 --> 00:00:51,360 It's a plain text file with ruby code embedded inside it. 17 00:00:52,490 --> 00:00:54,210 Between the opening HTML tag and 18 00:00:54,210 --> 00:00:58,530 the closing HTML tag is an HTML document which gets rendered as a web page. 19 00:00:59,650 --> 00:01:03,500 The head tag contains information about the entire document. 20 00:01:03,500 --> 00:01:06,270 Like the title that the browser should display. 21 00:01:06,270 --> 00:01:08,460 And here is the most important part of the layout. 22 00:01:08,460 --> 00:01:13,630 Within the body HTML tag is an ERB tag with the yield keyword. 23 00:01:13,630 --> 00:01:18,100 This will render another template and insert the result into this layout. 24 00:01:18,100 --> 00:01:22,020 Rails creates files from view templates in the app > views sub directory. 25 00:01:23,240 --> 00:01:28,370 Since this is a view for post resources, it will be within the posts sub directory. 26 00:01:28,370 --> 00:01:31,330 And since this is a view for the index controller action, 27 00:01:31,330 --> 00:01:34,750 it will be in the index.html.erb file. 28 00:01:35,750 --> 00:01:38,940 The template has a bunch of HTML tags that'll be inserted 29 00:01:38,940 --> 00:01:40,790 verbatim into the HTML page. 30 00:01:42,140 --> 00:01:46,090 ERB tags began with angle bracket percent sign and 31 00:01:46,090 --> 00:01:48,050 contain ruby code that gets interpreted. 32 00:01:49,130 --> 00:01:51,590 The result gets inserted into the HTML. 33 00:01:52,810 --> 00:01:55,810 Remember that back in the controller we assigned a collection of 34 00:01:55,810 --> 00:01:59,800 post objects to be posts instance variable. 35 00:01:59,800 --> 00:02:02,390 The call to @posts.each processes 36 00:02:02,390 --> 00:02:07,460 each of those posts adding a table row with the post title to the output HTML 37 00:02:07,460 --> 00:02:12,180 you can see the resulting HTML if we open our browser's developer tools. 38 00:02:12,180 --> 00:02:15,240 You'll be working with HTML templates a lot in Rails. 39 00:02:15,240 --> 00:02:18,120 If you don't have a firm understanding of HTML yet 40 00:02:18,120 --> 00:02:21,240 I'd recommend checking the teacher's notes to learn more. 41 00:02:21,240 --> 00:02:25,390 So when a browser requests a page Rails receives the request, 42 00:02:25,390 --> 00:02:29,150 routes it to the appropriate controller class and action method. 43 00:02:29,150 --> 00:02:32,170 The controller retrieve some model data and 44 00:02:32,170 --> 00:02:35,630 then the controller embeds the model data within a view. 45 00:02:35,630 --> 00:02:38,800 The results of rendering that view then gets returned to the browser 46 00:02:38,800 --> 00:02:39,590 as a response. 47 00:02:41,110 --> 00:02:45,120 Now you have an overview of how Rails handles a request from beginning to end. 48 00:02:45,120 --> 00:02:48,390 Over the remainder of this course we'll look at the components Rails uses 49 00:02:48,390 --> 00:02:49,680 in more detail. 50 00:02:49,680 --> 00:02:51,670 We'll start with models, and that's up next.