Hi,
What really happen is that ActionController::Base protects a number of specific
instance variables.
For rails 2.3.2 you can see the following somewhere in the source code:
cattr_reader :protected_instance_variables
# Controller specific instance variables which will not be accessible
inside views.
@@protected_instance_variables = %w(
@assigns
@performed_redirect
@performed_render
@variables_added
@request_origin
@url
@parent_controller
@action_name
@before_filter_chain_aborted
@action_cache_path
@_session
@_headers
@_params
@_flash
@_response
)
All these variables are not duplicated in the ActionView::Base instance object.
Yes, this is the
trick, a recreation of instance variables in the View of every single instance
variable defined in
the Controller, except for the list above.
Remenber, are object instance variables, so @action_name in the view is not
really the same
@action_name variable in the controller object. So you can redefine
@action_name in the view but
this has no relation with the other.
The recreation part of instance variables in the view is managed for this
method in the
ActionView::Base class:
def _copy_ivars_from_controller #:nodoc:
if @controller
variables = @controller.instance_variable_names
variables -= @controller.protected_instance_variables if
@controller.respond_to?
(:protected_instance_variables)
variables.each { |name| instance_variable_set(name,
@controller.instance_variable_get(name)) }
end
end
--Mansay
On Sat 02 May 2009 7:39:17 am westhielke wrote:
> Well, this is certainly getting interesting.
>
> I confirmed that using <%= controller.action_name %> in the template
> gives "index".
>
> However, if I use <%= @action_name %>, then I always get a blank,
> regardless whether I try to set the value of the instance variable
> myself or not.
> This would seem to indicate that the controller.action_name attribute
> is not the same as the instance variable @action_name. But, then why
> can't I create an instance variable called @action_name? Is it
> prohibited because of the controller.action_name attribute's
> existence? The action_name attribute is supposed to be read-write, so
> I should be able to set it, shouldn't I?
>
> I also tried using <%= action_name %> and this also gives "index".
>
> So, now I am confused regarding the naming of instance variables and
> class attributes, as well as how they are referenced from the view
> template-- plain "action_name" works (apparently referencing
> controller.action_name), but @action_name doesn't. The latter should
> be a reference to an instance variable, and this syntax does work when
> I use a different name. It's enlightening that I can use attributes of
> the controller by name (without any special name decoration) and that
> these are not the same as instance variables.
>
> On May 2, 2:23 am, Mansay <[email protected]> wrote:
> > Yes, it is,
> >
> > Look at the API
> >
> > http://api.rubyonrails.org/classes/ActionController/Base.html
> >
> > and make a search for "action_name". Yo will see that it is an attribute
> > of the class and indeed is almost sure supported by an instance variable
> > of the same name, making @action_name a bad choice for personal variables
> > and, as you could be thinking right now, totally innecesary.
> >
> > Also take a look at,
> >
> > http://api.rubyonrails.org/classes/ActionView/Base.html
> >
> > Search the documentation for the attributes of the class. You will find
> > there a "controller" attribute, then you can replace invocations in your
> > template as:
> >
> > Controller is <%= controller.name %>
> > Action was <%= controller.action_name %>
> >
> > --Mansay
> >
> > On Fri 01 May 2009 6:43:15 pm westhielke wrote:
> > > Hi,
> > >
> > > I've encountered a strange problem with exercise 1.5 of the Rails
> > > Action Controller lesson, which doesn't turn out as described in the
> > > exercise description.
> > >
> > > The controller has the statements:
> > >
> > > @controller_name = params[:controller]
> > > @action_name = params[:action]
> > >
> > > and the say_hello.rhtml has:
> > >
> > > Controller is <%= @controller_name %>
> > > <br/>
> > > Action was <%= @action_name %> !
> > > <br/>
> > >
> > > When I run it, the screen shows nothing for the action, i.e.,
> > >
> > > Controller is hello
> > > Action is
> > >
> > > I checked the log and the params[:action] is definitely "index".
> > >
> > > After some fooling around, I determined that it works fine, if I
> > > change the variable name to anything other than @action_name. For
> > > example, if I make it "@action_nam" or "@actionx", then it works.
> > >
> > > Any ideas why? Is "@action_name" a pre-defined variable or something,
> > > which gets set internally after the controller changes the value?
> > >
> > > --Wes
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
ruby-on-rails-programming-with-passion-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---