1 00:00:00,810 --> 00:00:04,136 This section of the log shows where Rails proceed to get requests for 2 00:00:04,136 --> 00:00:05,710 the posts path. 3 00:00:05,710 --> 00:00:10,010 The request gets sent to the index method of the PostsController class. 4 00:00:10,010 --> 00:00:13,040 The controller is responsible for handling the browser requests. 5 00:00:13,040 --> 00:00:16,770 It controls the model and the view to generate a response. 6 00:00:16,770 --> 00:00:18,949 The controller receives the request, 7 00:00:18,949 --> 00:00:22,522 loads the requested data via the model usually from a database. 8 00:00:22,522 --> 00:00:27,198 And then inserts that data into the view, which is usually an HTML page. 9 00:00:27,198 --> 00:00:31,300 The result of rendering the HTML is then returned to the browser as a response. 10 00:00:32,300 --> 00:00:35,190 Most of the code for your app including controllers, models and 11 00:00:35,190 --> 00:00:38,270 views will reside within the app sub directory. 12 00:00:38,270 --> 00:00:43,540 Rails create source files for controller classes in the app controllers directory. 13 00:00:43,540 --> 00:00:48,080 Here's the PostController class, in posts_controller.rb. 14 00:00:48,080 --> 00:00:51,410 And here's the index method or action. 15 00:00:51,410 --> 00:00:55,530 Controller methods that respond to requests are sometimes called actions. 16 00:00:55,530 --> 00:00:56,750 Doesn't look like much, but 17 00:00:56,750 --> 00:01:01,200 that's because it's mostly using the default operations to handle the request. 18 00:01:01,200 --> 00:01:04,590 First, it loads some data via the Post model class. 19 00:01:04,590 --> 00:01:08,230 It calls the Post.all method to load all post objects and 20 00:01:08,230 --> 00:01:10,940 stores them in an instance variable. 21 00:01:10,940 --> 00:01:15,000 Then the controller uses the data in that instance variable to render a view. 22 00:01:15,000 --> 00:01:18,180 By default it renders a template with the same name as the action 23 00:01:18,180 --> 00:01:21,620 within the same page layout the rest of the application uses. 24 00:01:21,620 --> 00:01:25,510 We could add an explicit call to the render method and get the same result. 25 00:01:25,510 --> 00:01:29,365 We could render the posts/index template using a layout of application. 26 00:01:33,396 --> 00:01:37,690 If we save this and then reload the page in our browser, we'll get the same result. 27 00:01:38,910 --> 00:01:42,560 So the controller loads model data by calling Post.all and 28 00:01:42,560 --> 00:01:44,480 then it renders the index view. 29 00:01:44,480 --> 00:01:46,900 Next, we'll look at that model and view in more detail.