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 -~----------~----~----~----~------~----~------~--~---
