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 ?

On Tue, Apr 22, 2008 at 2:24 PM, Arun [email protected] wrote:

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

On Apr 22, 7:49 pm, Arun [email protected] wrote:

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
Peak Obsession)

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

Quoting A. [email protected]:

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

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