Forum: Ruby on Rails How do templates get access to controller variables?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Jeff Cohen (Guest)
on 2006-02-25 06:06
(Received via mailing list)
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":

5. 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
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2006-02-25 06:49
(Received via mailing list)
On Sat, Feb 25, 2006 at 05:06:09AM -0000, Jeff Cohen 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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Jeff Cohen (Guest)
on 2006-02-25 15:31
(Received via mailing list)
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
www.softiesonrails.com
This topic is locked and can not be replied to.