Join Kevin Skoglund for an in-depth discussion in this video Instance variables, part of Ruby on Rails 3 Essential Training.
- View Offline
- Exercise Files
In the last movie, we saw how to embed Ruby code into our templates. Now we'll learn how to use instance variables to give our templates access to the data pulled together by the controller. Think back to our MVC diagram for a moment. I told you that the controller has one main function, which is to control things, but that has two parts to it. The first is to make decisions about how the flow of the application ought to work; the second function is that it needs to gather together data. It can make requests of our models in order to do that, but it's going to try and gather together all the information that it needs and then call the view template.
The view should have everything already set up, ready to go. All the view should have to worry about is how to present that data. A good way to think about it is to imagine a rock concert. We've got our guitar tech, who comes out and sets up all the guitars on the stage ahead of time, and there's different guitars for different songs, they may have different tunings and when the guitarists comes on stage to play, all guitarist has to do is walk over, pick up the guitar, and it's ready to go. The guitar tech took care of everything. That's the way it works between the controller and the view. So in order to make that handoff to pass things from the controller to the view, we're going to need to have a little bit of glue to hold things together, and the way that we're going to do that is using instance variables.
Now, just as a quick refresher, remember that there are regular variables in Ruby and then there're instance variables and the way we recognize an instance variable is it has the at sign in front of it. An instance variable is part of object- oriented programming and it says that this variable applies inside this instance of an object. So in our case, the controller is the object. It is just a regular Ruby class and therefore it creates an instance of that class and we can have instance variables in that class.
So the way the process works in Rails, is that we set an instance variable in the controller, and then when we go to render a template, Rails looks at the controller and gathers together all of its instance variables, all the things that have been set and makes those available to the template that it's about to render and those instance variables become the glue that bind the two together. So the way that we transfer information from our controller to our view is exclusively through these instance variables. Let's try them out. So let's open up demo_Controller again and we've been working with the hello actions.
We're going to stick with that. The first thing I want you to notice, is that the demo_Controller is just a regular Ruby class. So when a request comes in, Rails creates an instance of this class and therefore we can use instance variables. I am going to remove this redirect_to line and let's just create @array equals, and let's just make a real simple array, all right, 1, 2, 3, 4, 5, just a very simple Ruby array. Now, notice that I'm using an instance variable with the at sign, not a regular variables without the at sign.
By doing this, now my instance of demo _Controller has an instance variable called array, and therefore when I render the Hello template, here I have access to it. So inside my Hello template, let's just change this. So instead of times do, let's change it to at @array each do and for each number we'll just change it so that outputs a number followed by a br tag. All right, so that will go through each number in the array, output it, and put a br tag after it. Let's load it up.
You want to make sure that your web server is running, mine still is, open up Firefox, demo/hello, and there we go. That's all there is to it. So if you set an instance variable, it now becomes available to us in the View. The very last point that I want to make is that there is this very cozy relationship between the controller and the view. In fact their code is actually grouped together inside the Rails framework as part of Action Pack. ActionController and ActionView are both part of Action Pack. But even though they're very cozy, don't be deceived into thinking that somehow you can go from the view back to the controller.
It doesn't work that way. You can't go back and get something. The controller has to set it up all ahead of time, so that the view then can just present it and just send it back to the browser and move on. Maybe an obvious point, but I just want to make sure that you realize that that arrow does go one way.
- 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.