Constructors

Hi all,

A little question. How can u use an constructor in controller? I would
like to initialize some central stuff before i’m processing the
controller.

thanks in advance,

Martijn

Martijn van Leeuwen wrote:

Hi all,

A little question. How can u use an constructor in controller? I would
like to initialize some central stuff before i’m processing the controller.
Use a before_filter.

You probably actually want to use a before_filter to perform some
initialization before performing an action, rather than use a
constructor at all… unless you have some very odd requirements.

  • james

On 1/24/06, James A. [email protected] wrote:

You probably actually want to use a before_filter to perform some
initialization before performing an action, rather than use a
constructor at all… unless you have some very odd requirements.

I don’t understand Rails internals at at all… so I’m curious.

Why wouldn’t a constructor with a call to super work?

Joe

I’m not sure about controllers, but I found the hard way that Rails
disconnects model initialize methods for performance purposes…

Perhaps the controllers are the same?


– Tom M.

On 1/25/06, James A. [email protected] wrote:

Constructor objects are actually created as part of the dispatcher
process (dispatcher.rb, around line 38):

Do you mean “Controller objects”?

Joe

Yes - d’oh! :slight_smile:

Constructor objects are actually created as part of the dispatcher
process (dispatcher.rb, around line 38):

ActionController::Routing::Routes.recognize!(request).process(request,
response).out(output)

Basically, deep within the routing system the code generation
performed by routing instantiates the relevant ApplicationController
subclass (that call to recognize!() returns it, as I recall), and it
needs to have control over which arguments are given. Joo don’ wanna
mess wit dat.

  • james

I’m not actually sure that it will - my understanding of the angle it
is taking is a discussion of Ruby from the perspective of a Rails
developer, since Rails has made popular a number of ruby ‘tricks’,
including:

using :symbols instead of ‘strings’
collecting method arguments => into, a => hash, without => {using =>
curly_braces}

and so on. Ruby for people who came to Rails not knowing anything
about Ruby. The Rails internals can be mystifying even for me, and
I’ve been using Ruby since 2002 :slight_smile:

  • james

On 1/25/06, James A. [email protected] wrote:

Yes - d’oh! :slight_smile:

I hope David Black’s upcoming Rails book will illuminate this Rails
voodoo to me. I understand Ruby fairly well, but the Rails internals
are still magic.