<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: ActionController and what the heck is attr_internal?</title>
	<atom:link href="http://rubyonrailswin.wordpress.com/2007/03/07/actioncontroller-and-what-the-heck-is-attr_internal/feed/" rel="self" type="application/rss+xml" />
	<link>http://rubyonrailswin.wordpress.com/2007/03/07/actioncontroller-and-what-the-heck-is-attr_internal/</link>
	<description>Sharing my experience in Ruby on Rails development</description>
	<lastBuildDate>Fri, 30 Oct 2009 10:51:23 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Linan Wang</title>
		<link>http://rubyonrailswin.wordpress.com/2007/03/07/actioncontroller-and-what-the-heck-is-attr_internal/#comment-104</link>
		<dc:creator>Linan Wang</dc:creator>
		<pubDate>Mon, 15 Sep 2008 00:34:37 +0000</pubDate>
		<guid isPermaLink="false">http://rubyonrailswin.wordpress.com/2007/03/07/actioncontroller-and-what-the-heck-is-attr_internal/#comment-104</guid>
		<description>@Raul
Thanks for the explanation. it&#039;s still helpful even more than 1 year later!</description>
		<content:encoded><![CDATA[<p>@Raul<br />
Thanks for the explanation. it&#8217;s still helpful even more than 1 year later!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raul Parolari</title>
		<link>http://rubyonrailswin.wordpress.com/2007/03/07/actioncontroller-and-what-the-heck-is-attr_internal/#comment-26</link>
		<dc:creator>Raul Parolari</dc:creator>
		<pubDate>Fri, 06 Jul 2007 02:15:35 +0000</pubDate>
		<guid isPermaLink="false">http://rubyonrailswin.wordpress.com/2007/03/07/actioncontroller-and-what-the-heck-is-attr_internal/#comment-26</guid>
		<description>Hi, Bob,

I am interested in Rails internals too (and in particular at how the Controller and View use metaprogramming to share information) and I spent some time studying the code in this area; although not a Rails expert, I can offer this explanation of what you saw:

1) first a caveat: you mention that you were testing “in a View”, and then you looked at the Controller code. Controller and View are different classes, so don’t assume that we can just browse the controller code to explain what happens in the view (actually: as the Controller transmits some variables to the view, this will work some of the times; what will make things even more confusing!).

2) Why writing ‘controller.action_name’ in the view worked?
When the controller instantiates the view, it passes itself (’self’). The view stores this parameter as instance variable @controller (see method initialize); it also declares ‘attr_accessor :controller’, and this explains that you were able to write ‘controller.action_name’ (note: we are really calling method ‘action_name’ in the controller).
However, ‘action_name’ by itself does not exist in the View, and that is the reason that it failed when you tried (actually… it exists as ‘@action_name’, but forget this: the standard method is to retrieve it via params).

3) Let’s come to the heart of your question: what is attr_internal? it is not very different from attr_accessor, but with this small difference: when you write:

attr_accessor :params # methods to read/write instance var @params
attr_internal :params # methods to read/write instance var @_params

Why did the Rails team go to the trouble of doing this? they want users to call a method (params in the example) and not access any more the instance variables [actually, for the moment they still make available @params &amp; co, but flooding the logs with deprecation warnings]. This will make possible to change the implementations of params (and session, request, etc) without impact in existing software; of course, the variable @_params is still technically accessible to the user, but the ‘_’ should signals users that it is not to be used.
Thus, the meaning of ‘internal’ means: use the method, not the variable!

4) Why does setting @@view_controller_internals=nil causes Rails to malfunction? The reason (a bit silly) is the following:
a) the controller transmits (just before invoking render) a snapshot of some of its variables to the view in a hash (look at controller method ‘add_instance_variables_to_assigns’), unless they are ‘protected’.
b) now,if view_controller_internals is false, ‘protected’ includes almost all the variables of interest (including the ‘internal’ ones, but not only); so the View does not get any information!
I don’t know the reason why this is present in the code (perhaps a feature being developed? if the controller wants to hide its variables, it surely needs another mechanism to transmit information to the view).

5) Finally: you compared &#039;self.object_id&#039; in the controller with &#039;controller.object_id&#039; in the View and wondered if &#039;a controller object is instantiated before it gets to the controller code&#039;. 
No, things are simple here; from point 2) in my answers, you can see that the values were the same because... in fact they are the same objects! controller.object_id in the view is a call to method &#039;object_id&#039; in the controller (the controller passed its identity to the view when instantiating it).  

There is a lot more to say on how information is manipulated between Controller and View, but I hope that this helped to shed some light on the points you raised,
Raul</description>
		<content:encoded><![CDATA[<p>Hi, Bob,</p>
<p>I am interested in Rails internals too (and in particular at how the Controller and View use metaprogramming to share information) and I spent some time studying the code in this area; although not a Rails expert, I can offer this explanation of what you saw:</p>
<p>1) first a caveat: you mention that you were testing “in a View”, and then you looked at the Controller code. Controller and View are different classes, so don’t assume that we can just browse the controller code to explain what happens in the view (actually: as the Controller transmits some variables to the view, this will work some of the times; what will make things even more confusing!).</p>
<p>2) Why writing ‘controller.action_name’ in the view worked?<br />
When the controller instantiates the view, it passes itself (’self’). The view stores this parameter as instance variable @controller (see method initialize); it also declares ‘attr_accessor :controller’, and this explains that you were able to write ‘controller.action_name’ (note: we are really calling method ‘action_name’ in the controller).<br />
However, ‘action_name’ by itself does not exist in the View, and that is the reason that it failed when you tried (actually… it exists as ‘@action_name’, but forget this: the standard method is to retrieve it via params).</p>
<p>3) Let’s come to the heart of your question: what is attr_internal? it is not very different from attr_accessor, but with this small difference: when you write:</p>
<p>attr_accessor :params # methods to read/write instance var @params<br />
attr_internal :params # methods to read/write instance var @_params</p>
<p>Why did the Rails team go to the trouble of doing this? they want users to call a method (params in the example) and not access any more the instance variables [actually, for the moment they still make available @params &amp; co, but flooding the logs with deprecation warnings]. This will make possible to change the implementations of params (and session, request, etc) without impact in existing software; of course, the variable @_params is still technically accessible to the user, but the ‘_’ should signals users that it is not to be used.<br />
Thus, the meaning of ‘internal’ means: use the method, not the variable!</p>
<p>4) Why does setting @@view_controller_internals=nil causes Rails to malfunction? The reason (a bit silly) is the following:<br />
a) the controller transmits (just before invoking render) a snapshot of some of its variables to the view in a hash (look at controller method ‘add_instance_variables_to_assigns’), unless they are ‘protected’.<br />
b) now,if view_controller_internals is false, ‘protected’ includes almost all the variables of interest (including the ‘internal’ ones, but not only); so the View does not get any information!<br />
I don’t know the reason why this is present in the code (perhaps a feature being developed? if the controller wants to hide its variables, it surely needs another mechanism to transmit information to the view).</p>
<p>5) Finally: you compared &#8217;self.object_id&#8217; in the controller with &#8216;controller.object_id&#8217; in the View and wondered if &#8216;a controller object is instantiated before it gets to the controller code&#8217;.<br />
No, things are simple here; from point 2) in my answers, you can see that the values were the same because&#8230; in fact they are the same objects! controller.object_id in the view is a call to method &#8216;object_id&#8217; in the controller (the controller passed its identity to the view when instantiating it).  </p>
<p>There is a lot more to say on how information is manipulated between Controller and View, but I hope that this helped to shed some light on the points you raised,<br />
Raul</p>
]]></content:encoded>
	</item>
</channel>
</rss>
