init for controller

I'd like to execute some code only during first invocation of
controller - sort of a init method. The subsequent invocations do not
need to invoke this code.

How do I achieve it ?

Um, what is it you want to do? Each call to a given controller's
action is in a brand new controller instance, so any initialize method
is useless.

If it's one-time initialization of something, then you want
config.after_initialize in your environment.rb

Jason

> How do I achieve it ?

Um, what is it you want to do? Each call to a given controller's
action is in a brand new controller instance, so any initialize method
is useless.

I'm trying to port some Java EE Servlet code to Rails controller/
action and am trying to find out the best place for Servlet.init()
method.

If it's one-time initialization of something, then you want
config.after_initialize in your environment.rb

Ah, can you provide some docs/examples of that ?

-Arun

> > How do I achieve it ?

> Um, what is it you want to do? Each call to a given controller's
> action is in a brand new controller instance, so any initialize method
> is useless.

I'm trying to port some Java EE Servlet code to Rails controller/
action and am trying to find out the best place for Servlet.init()
method.

in config/initializers?

Fred

You can use the before_filter class method which will execute code
before any action is done in your controller. I'm not sure what you
mean by "first invocation of a controller" since as Jason pointed out
each call to a controller creates a new instance.

Can you explain more? (For details on the filters look here
http://api.rubyonrails.com/classes/ActionController/Filters/ClassMethods.html)

Nicholas

> I'm trying to port some Java EE Servlet code to Rails controller/
> action and am trying to find out the best place for Servlet.init()
> method.

in config/initializers?

So what do I define in config/initializers and how do I invoke it ?

-Arun

Ok figured out that all *.rb files are read in config/initializers so
that is working. But

greeting = "this is it!"

or

@greeting = "this is it!"

does not allow me to access it in the view.

How do I define my "public static final" constants here ?

-Arun

Quoting Arun <arun.gupta@gmail.com>:

Ok figured out that all *.rb files are read in config/initializers so
that is working. But

greeting = "this is it!"

or

@greeting = "this is it!"

does not allow me to access it in the view.

How do I define my "public static final" constants here ?

Is it a class variable or an instance variable? If the former, just add
"GREETING = "this is it!" after the class statement and before any def
statements. If the latter, you need to describe more how it is used.

HTH,
  Jeffrey