Forum: Ruby on Rails Constructors

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
234baba7870c09d73a2805438a75d36d?d=identicon&s=25 Martijn van Leeuwen (Guest)
on 2006-01-24 17:06
(Received via mailing list)
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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-24 17:13
(Received via mailing list)
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.
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-01-24 18:01
(Received via mailing list)
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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-24 19:33
(Received via mailing list)
On 1/24/06, James Adam <james.adam@gmail.com> 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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-01-24 20:25
(Received via mailing list)
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 Mornini
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-01-25 14:52
(Received via mailing list)
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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-25 16:26
(Received via mailing list)
On 1/25/06, James Adam <james.adam@gmail.com> wrote:
> Constructor objects are actually created as part of the dispatcher
> process (dispatcher.rb, around line 38):

Do you mean "Controller objects"?

Joe
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-01-25 17:08
(Received via mailing list)
Yes - d'oh! :)
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-25 18:06
(Received via mailing list)
On 1/25/06, James Adam <james.adam@gmail.com> wrote:
> Yes - d'oh! :)

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.
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-01-26 11:21
(Received via mailing list)
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 :)

- james
This topic is locked and can not be replied to.