The View2:52 with Jay McGavren
The view is responsible for displaying data to your users.
You'll be working with HTML templates a lot in Rails. If you don't have a firm understanding of HTML yet, I'd recommend checking out the How to Make a Website course.
We're back on the rails log again. 0:00 Rails controllers insert load of model data into a view 0:02 to create a response for the browser. 0:05 The view is responsible for displaying data to your users. 0:08 Views usually though not always take the form of HTML templates with Ruby 0:11 expressions embedded in them. 0:16 Those expressions refer to data loaded from the model 0:18 to populate an HTML page with data. 0:20 This line in our controller renders the view. 0:24 And remember, 0:27 even if we remove this line again the same thing would happen by default. 0:28 This layout application parameter here instructs 0:31 Rails to load App > Views > Layouts > application.html.erb. 0:37 This is an ERB template. 0:43 ERB stands for embedded ruby. 0:45 It's a plain text file with ruby code embedded inside it. 0:48 Between the opening HTML tag and 0:52 the closing HTML tag is an HTML document which gets rendered as a web page. 0:54 The head tag contains information about the entire document. 0:59 Like the title that the browser should display. 1:03 And here is the most important part of the layout. 1:06 Within the body HTML tag is an ERB tag with the yield keyword. 1:08 This will render another template and insert the result into this layout. 1:13 Rails creates files from view templates in the app > views sub directory. 1:18 Since this is a view for post resources, it will be within the posts sub directory. 1:23 And since this is a view for the index controller action, 1:28 it will be in the index.html.erb file. 1:31 The template has a bunch of HTML tags that'll be inserted 1:35 verbatim into the HTML page. 1:38 ERB tags began with angle bracket percent sign and 1:42 contain ruby code that gets interpreted. 1:46 The result gets inserted into the HTML. 1:49 Remember the back in the controller we assigned a collection of 1:52 post objects to be post instance variable. 1:55 The call to post.each processes 1:59 each of those posts adding a table row with the post title to the output HTML 2:02 you can see the resulting HTML if we open our browsers developer tools. 2:07 You'll be working with HTML templates a lot in Rails. 2:12 If you don't have a firm understanding of HTML yet 2:15 I'd recommend checking the teachers notes to learn more. 2:18 So when a browser requests a page Rails receives the request, 2:21 routes it to the appropriate controller class and action method. 2:25 The controller retrieve some model data and 2:29 then the controller embeds the model data within a view. 2:32 The results of rendering that view then gets returned to the browser 2:35 as a response. 2:38 Now you have an overview of how Rails handles a request from beginning to end. 2:41 Over the remainder of this course we'll look at the components Rails uses 2:45 in more detail. 2:48 We'll start with models, and that's up next. 2:49
You need to sign up for Treehouse in order to download course files.Sign up