# How do templates get access to controller variables?

**URL:** <https://www.ruby-forum.com/t/how-do-templates-get-access-to-controller-variables/73149>\
**Category:** Rails\
**Created:** [February 25, 2006, 6:06am UTC](https://www.ruby-forum.com/t/how-do-templates-get-access-to-controller-variables/73149 "2006-02-25T06:06:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 25, 2006, 6:06am UTC](https://www.ruby-forum.com/t/how-do-templates-get-access-to-controller-variables/73149/1 "2006-02-25T06:06:58Z")

</div>

This is probably more of a Ruby question than a Rails question. I’ve  
always wondered exactly how controller instance variables are being made  
accessible to the ERb code in the views.

In the Agile Rails book on pages 38, there’s a sidebar at the top that  
hints at what’s going on:

“Rails does some Ruby magic so that the instance variables of the  
controller object are injected into the tmeplate object”.

and then on the next page, this sentence appears as part of the “story  
so far”:

1. Rails processes this template through ERb… substituting in values  
set up by the controller.

I’m really interested in learning what Ruby language features made this  
cool stuff possible. In general, how can I get one object to “know  
about” or get references to private attributes of another object?

Thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 25, 2006, 6:49am UTC](https://www.ruby-forum.com/t/how-do-templates-get-access-to-controller-variables/73149/2 "2006-02-25T06:49:23Z")

</div>

On Sat, Feb 25, 2006 at 05:06:09AM -0000, Jeff C. wrote:

> This is probably more of a Ruby question than a Rails question. I’ve  
> always wondered exactly how controller instance variables are being made  
> accessible to the ERb code in the views.

Essentially a combination of Object#instance\_variables and  
Object#instance\_variable\_get, from one object (the controller instance)  
to  
another (the controller’s corresponding template object).

Check out add\_instance\_variables\_to\_assigns and its general area of  
ActionController::Base.

actionpack/lib/action\_controller/base.rb

They are basically just copied over one by one, save for a few which are  
skipped for the sake of encapsulation.

marcel

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [February 25, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/how-do-templates-get-access-to-controller-variables/73149/3 "2006-02-25T15:31:57Z")

</div>

On Saturday, February 25, 2006, at 12:01 AM, Marcel Molina Jr. wrote:

> Check out add\_instance\_variables\_to\_assigns and its general area of  
> ActionController::Base.
> 
> actionpack/lib/action\_controller/base.rb
> 
> They are basically just copied over one by one, save for a few which are  
> skipped for the sake of encapsulation.
> 
> marcel

Thanks Marcel, that helps a lot.

By the way, thanks for the presentation on testing you did at the  
Chicago ruby user’s group recently. I got a lot out of it.

Jeff

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software
