One thing that I find very interesting is that a lot of people struggle with why do we gave to have these variables with @ symbols at the beginning, when we could just a regular variable without them. And Rails uses these instance variables so that we can use them in our controller and access them over here in our views. Now why would they do something like that? It seems weird but when you actually understand the internals of Rails, that it's actually just a Ruby program. You start to realize that maybe this is a little simpler than you thought it was. There's really no magic going on here.
Imagine this, a request comes into Rails, and they create a new instance of this blog controller, so we'll say that, inside Rails:
kontroller = BlogController.new
and then they call the show method
This is going to tell the blog controller instance to set the id and test variables and you can imagine that it allows you to say
kontroller.test and so you can access those variables from inside Rails and the reason why that's important is because when you render these views, we have a thing called embedded ruby, which is why the file name is erb, and you can render these views and you would say: "Okay, what action do we want to render?", well we want to render show.html.erb, and we want these variables to be passed in because erb is not really useful unless you run some ruby in there, and put some real data in there. So what they do is they say the kontroller goes in there. This is very pseudo code, Rails doesn't exactly work like this, but this is generally what happens. We create a new copy of the blog controller, we execute the show action and then we tell erb to render the show with the variables inside the controller, now what they do here is that they take the instance variables, and the variables with the @ symbol at the beginning it's called an instance variable, so these are the ones that are only set for this instance of a controller, or a class. So these are regular ruby classes that we're working with, and rails is just getting all of the variables of this instance of this controller and pass them into our views, so when we do that, it just looks at all the @ symbol variables and sends them in, which is why they can be shared across your views. If you were to do a regular variable, (for example:
id = params[:id]), this variable is going to get lost as soon as the show action is done executing, so it doesn't save it into the instance of the blog class, so this gets lost in the ether as soon as the show action is done executing, but these variables, the instance variables stay around until the controller itself is destroyed, so these variables stay around, and that's why we need to have them. So we need to save those so that later on you can pass them into your views, and if you're not familiar with this, definitely take a look at resources online that you can find and I'll put them in the notes for this episode. That can teach you more about instance variables and how classes work.
This is something that’s very important to understand but also gives you a glimpse into Rails that this is regular ruby code, there's no real magic going on here. It's just running ruby code as you would imagine it. Rails itself is nothing really special, they've just taken a lot of this stuff and allowed you to just imagine and focus on what you want to build rather than having to deal with creating a new controller and sending an action and then rendering views. You don't have to deal with that, they've taken care of it, but they've used features of ruby that you need to understand to really appreciate what's going on in each of these methods.
Transcript written by Miguel
Join 18,000+ developers who get early access to new screencasts, articles, guides, updates, and more.