Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use instance variables to "connect" controllers with views?

This is a conceptual question and I haven't been able to find the answer in SO, so here I go:

Why instance variables are used to connect controllers and views? Don't we have two different objects of two different classes (Controller vs Views). So, when the view is rendered we are in a different context, but we are using instance variables of another object? Isn't this breaking encapsulation in somehow?

How does Rails manage to do that matching from one object to another? Does it clone all the instances variables of the controller to the view?

like image 470
Nobita Avatar asked Feb 21 '12 20:02

Nobita


People also ask

Why do we need instance variables?

Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class. Instance variables can be declared at the class level before or after use. Access modifiers can be given for instance variables.

Are instance variables set within a controller method accessible within a view?

Are instance variables set within a controller method accessible within a view? Yes, any instance variables that are set in an action method on a controller can be accessed and displayed in a view.

When Should a variable be an instance variable?

Instance Variable: These variables are declared within a class but outside a method, constructor, or block and always get a default value. These variables are usually created when we create an object and are destroyed when the object is destroyed.

Why do we need instance variables in Java?

An Instance variable in Java is used by Objects to store their states. Variables that are defined without the STATIC keyword and are Outside any method declaration are Object-specific and are known as instance variables. They are called so because their values are instance-specific and are not shared among instances.


2 Answers

In a sense, you could say that it is breaking encapsulation. I have found that if you are not careful, it is easy to get your business/presentation logic mixed together in Rails. It usually starts when I am writing a view template, and discover that I need some value which I didn't pass from the controller. So I go back, and tweak the controller to suit what I need in the view. After one tweak, and another, and another, you look at the controller method, and it is setting all kinds of instance variables which don't make sense unless you look at the view to see what they are for. So you end up in a situation where you need to look at both controller and view to understand either, rather than being able to take one or the other in isolation.

I think that using instance variables (together with the Binding trick) is simply a way to pass whatever values you need from controller to view, without having to declare parameters in advance (as you would when defining a method). No declarations means less code to write, and less to change when you want to refactor and reorganize things.

like image 52
Alex D Avatar answered Sep 27 '22 19:09

Alex D


Rails uses eval and Binding to pass controller instance variables to views. See this presentation from Dave Thomas, there's a small example at minute 46' that explains how this is done.

like image 35
mbreining Avatar answered Sep 27 '22 20:09

mbreining