Constructors


#1

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


#2

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.


#3

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

#4

On 1/24/06, James A. removed_email_address@domain.invalid 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


#5

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.


#6

On 1/25/06, James A. removed_email_address@domain.invalid wrote:

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

Do you mean “Controller objects”?

Joe


#7

Yes - d’oh! :slight_smile:


#8

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

#9

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

#10

On 1/25/06, James A. removed_email_address@domain.invalid 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.