Is it possible to declare an instance variable in a controller that is available to every action without defining the variable in every action? -- Posted via http://www.ruby-forum.com/.
On 6/5/06, ryan <ryan@air-nett.com> wrote:> > Is it possible to declare an instance variable in a controller that is > available to every action without defining the variable in every action?You have two main options (though there are others): The firstis to use a session variable: session[:name] = value The other is a "class variable": @@variable = value That will create a "class variable" which is available to every instance of the controller. However, because the Rails environment will get reloaded on each request in development mode, the value won''t persist across requests. In production mode, I think the behavior is indeterminate and depends on your setup. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060605/0b3fbcf6/attachment.html
ryan wrote:>Is it possible to declare an instance variable in a controller that is >available to every action without defining the variable in every action? > > >You could do it in a before_filter. -- Jack Christensen jackc@hylesanderson.edu
Thanks for the help. I just found what I wanted. I didn''t need persistence across actions, but actually to just repeat some code for every action (but I didn''t want to actually duplicate code.) I used the initialize() fuction in the controller class. Justin Bailey wrote:> On 6/5/06, ryan <ryan@air-nett.com> wrote: >> >> Is it possible to declare an instance variable in a controller that is >> available to every action without defining the variable in every action? > > > You have two main options (though there are others): > > The firstis to use a session variable: > > session[:name] = value > > The other is a "class variable": > > @@variable = value > > That will create a "class variable" which is available to every instance > of > the controller. However, because the Rails environment will get reloaded > on > each request in development mode, the value won''t persist across > requests. > In production mode, I think the behavior is indeterminate and depends on > your setup.-- Posted via http://www.ruby-forum.com/.
Ryan: Consider using before_filter instead of initialize (It''s a good habit and you may want to do something else in initialize, or use the same code over and over) application.rb def do_stuff @variable_to_display = "hello world" end store_contrller.rb before_filter :do_stuff login_controller.rb before_filter :do_stuff, :except=>[:login, :logout] On 6/5/06, ryan <ryan@air-nett.com> wrote:> Thanks for the help. > > I just found what I wanted. I didn''t need persistence across actions, > but actually to just repeat some code for every action (but I didn''t > want to actually duplicate code.) > > I used the initialize() fuction in the controller class. > > > > Justin Bailey wrote: > > On 6/5/06, ryan <ryan@air-nett.com> wrote: > >> > >> Is it possible to declare an instance variable in a controller that is > >> available to every action without defining the variable in every action? > > > > > > You have two main options (though there are others): > > > > The firstis to use a session variable: > > > > session[:name] = value > > > > The other is a "class variable": > > > > @@variable = value > > > > That will create a "class variable" which is available to every instance > > of > > the controller. However, because the Rails environment will get reloaded > > on > > each request in development mode, the value won''t persist across > > requests. > > In production mode, I think the behavior is indeterminate and depends on > > your setup. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks for the input. I suppose before_filter would be more flexible than initialize. Brian Hogan wrote:> Ryan: > > Consider using before_filter instead of initialize (It''s a good habit > and you may want to do something else in initialize, or use the same > code over and over) > > application.rb > > def do_stuff > @variable_to_display = "hello world" > end > > store_contrller.rb > > before_filter :do_stuff > > login_controller.rb > > before_filter :do_stuff, :except=>[:login, :logout]-- Posted via http://www.ruby-forum.com/.