Join Kevin Skoglund for an in-depth discussion in this video View templates, part of Ruby on Rails 3 Essential Training.
We've seen how controllers can either redirect or render a view. Now I would like to take a closer look at how the View Templates function. So far our View Templates have been composed of just HTML and we could use only HTML but that won't be dynamic or database driven. We might as well just use static HTML pages for the regular web site, but you could. You could use Rails routing and the controller to pick among a bunch of static pages. That's not very exciting and we're going to shoot for something a little bigger than that. What we want instead is to be able to embed Ruby code into our templates.
That way we will be able to embed dynamic information into them. In order to do that, we're going to make use of ERb. ERb is short for embedded Ruby, and that's exactly we want to be able to embed Ruby code. It's an eRuby templating system to embed Ruby. Now, you may have seen these letters ERb pop up already, because it's part of our template file name. hello.html.erb. What that is, is we have the template named hello, we're going to process it with the ERb and when we're done, the final format will be in HTML.
We have the less than sign with a percent, followed by our Ruby code, and then a percent and the greater than sign. Those two tags bracket our code. There is a variation on it, which is that we put an equal sign after the first percent and that outputs the results of the code. So that's the difference. The first will simply execute Ruby code; the second will execute it and output what it returns. Once we try both, the difference will be very clear. So the first thing I want to do is open up demo_controller and let's just comment out this redirect_to lynda.com line, so that hello now will give us our template again.
So that's the first thing. So then we want to go to our hello template. You can see it's got the .erb at the end, so it's ready to be processed by ERb. Let's open that up and let's actually embed something in it. Let's try doing just 1 + 1, all right. So there is inside those tags. I'll save the document. Let's make sure our web server is running, mine is still running from before. If not you'll want to start yours, and let's open up our web browser and instead of going to other_hello, I'll just go to demo/hello. So, there it is. It output Hello World! It did not put 1 + 1, there's no 2 anywhere on that page, and if we actually view the source for it as well, you'll see that it's not there.
It's not on the page. The reason why is because it did add 1 + 1, but then it didn't do anything with it. If we wanted to output the result, we need to put the equal sign there. So I'll just save it. Let's go back to the page. Let's try it again and sure enough, this time it does output the 2. So as another example, let's jump down here and let's make a new one where we say target equals and we'll make it world, and then jump down here, and we'll say what we want to output is hello and target.
So this is just basic Ruby, being able to embed a variable inside a double quoted string. It has to be double quotes or it won't work. So this is just fundamental Ruby and notice that we're outputting at this time. This time we didn't, so take a look at that and just make sure that you know what to expect and then come over and let's just reload the page. You'll see that we get back Hello World. So that's all there is to being able to embed Ruby code into our templates. It's that easy. Now, there are some subtleties to it and I just want to make sure that I point out one of them. For example, if we have a valid Ruby like 3.times do puts, and we'll put xxx.
All right so that should output three times xxx. Let's come right to our web browser and I'll just try that. It did not put out the Xs. The Xs show up here in our Terminal output. So puts, we cannot use inside ERb Templates. So if you're used to working in Ruby, that's one big change you need to be aware of. You cannot use puts inside here. Same thing with gets. Instead the right way to do this is to use the output tag, and we don't want to just put the output tag there. What you do instead is end one tag and then instead of puts, there we go, we're going to output it like that and then put the tags around end as well, okay.
So that's the right way to do it inside an ERb Template. We have the beginning and the ending of the loop here and then we have what we want to actually output here. And in practice that's often the case because we might want to have something here like aaa for example, right? So let's just try that, see the difference, and there we go. It actually outputs it there instead of to our console. The last note I want to make is that it used to be a common and meaningful thing for people to put minus signs either right after or right before the percent sign and what that did was it suppressed an automatic line return in the source code, when it actually output the HTML.
So it made for prettier HTML output. That's now the default. So we don't have to specify it any more. We get that automatically. So there is no HTML line return built into these. You can actually go back to the HTML and take a look and see that's true. So if you have used a previous version of Rails and you seen these minus signs before, we no longer have to do them.
- Understanding MVC (Model View Controller ) architecture
- Routing browser requests through the framework
- Responding to requests with dynamic content
- Defining associations and database relationships
- Creating, reading, updating and deleting records
- Working with forms
- Validating form data
- Reviewing built-in security features
- Authenticating users and managing user access
- Debugging and error handling
Skill Level Beginner
1. What Is Ruby on Rails?
2. Installing Ruby on Rails on a Mac
3. Installing Ruby on Rails on a Windows Machine
4. Getting Started
5. Controllers, Views, and Dynamic Content
6. Databases and Migrations
7. Models, ActiveRecord, and ActiveRelation
9. Controllers and CRUD
10. Layouts, Partials, and View Helpers
12. Data Validation
13. User Authentication
14. Improving the Simple CMS
15. Debugging and Error Handling
16. Introducing More Advanced Topics
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.