Session weirdness


#1

Hi,

If I put @fname = session[:user].first_name in the initialize method of
a controller, I get nil ojbect error. However, when I put the exact
same code in the template called by that controller (hard-coded
initialize to get past error) such as <%= session[:user].first_name %>
it works just fine.

Also, in the ‘Show Session Dump’ of the error page reporting the nil
object, the user attribute of ‘first_name’ is there :frowning:

What would cause initialize of a controller to not be able to see
session variables?

Thanks!
John


#2

Hi Craig,

CSN wrote:

Perhaps try putting a @ in front of session. But
(somebody correct me if I’m wrong), I don’t think vars
like @params, @request, @session are available in
initialize methods. I tried using @request.env in an
initialize method but got a nil object error, so
instead I put it in another method and added a
before_filter. If these vars aren’t available in
initialize methods, I’m curious why.

I was using @session before, but somebody on IRC told me that @session
has been deprecated in favor of just session. Either way, it doesn’t
work in initialize as is.

So, as you suggested, I created a before filter below and it works now:

before_filter :init

def init
@domain = session[:user].domain_name
end

So, the bigger question for me now is why would @params, @request,
@session not be available in initialize (as you found)? Anybody?

Thanks,
John


#3

Perhaps try putting a @ in front of session. But
(somebody correct me if I’m wrong), I don’t think vars
like @params, @request, @session are available in
initialize methods. I tried using @request.env in an
initialize method but got a nil object error, so
instead I put it in another method and added a
before_filter. If these vars aren’t available in
initialize methods, I’m curious why.

csn

— John D. removed_email_address@domain.invalid wrote:

it works just fine.
Thanks!
John


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

craig


Yahoo! FareChase: Search multiple travel sites in one click.


#4

Hello all,

I’m going through a few tutorials that I’d found online (and any extras
that anyone can suggest would be appreciated!). I’m looking at one which
is talking about using script/generate scaffold to actually generate the
files. When I run script/generate scaffold Todo, the files that are
generated are todos_controller.rb, and a number of files in views/todos.
I’m a little confused why it’s creating files in todos rather than todo
as the tutorials are showing. Any ideas?

Thanks,

Alastair


#5

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 14, 2005, at 2:51 PM, Alastair M. wrote:

I’m going through a few tutorials that I’d found online (and any
extras that anyone can suggest would be appreciated!). I’m looking
at one which is talking about using script/generate scaffold to
actually generate the files. When I run script/generate scaffold
Todo, the files that are generated are todos_controller.rb, and a
number of files in views/todos. I’m a little confused why it’s
creating files in todos rather than todo as the tutorials are
showing. Any ideas?

The tutorials are old: when the controller name is omitted, the
generator now pluralizes the model name by default.

For a singular name (or any other name), provide it explicitly:

script/generate scaffold Todo Todo
script/generate scaffold Todo QuePasaHombre

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDeRW1AQHALep9HFYRAragAKClu+fg/yEewL6RcTKaTsvmb0toCQCeMvTq
Oo3yWD5YHWZD/e4Bxp6YQfY=
=X7Sg
-----END PGP SIGNATURE-----


#6

Jeremy K. wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 14, 2005, at 2:20 PM, John D. wrote:

So, the bigger question for me now is why would @params, @request,
@session not be available in initialize (as you found)? Anybody?

The initialize method is used to … initialize the controller. It’s
where these instance variables are created for your use. Use
before_filters rather than override it.

Thanks for the clarification! Sorry, I’m a rails and ruby noob and it
was not obvious to me why session would be out of scope of the
initialize.

Further clarification, can I assume then that a before_filter actually
runs after the initialize method?

If yes, this makes me curious about how things flow. I am using
login_generator and I have before_filter :login_required, so if I wasn’t
logged in, this would imply that the initialize method for the current
controller runs even though I get redirected to the login page. Yes?

Thanks again for your valuable feedback!

Regards,
John


#7

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 14, 2005, at 2:59 PM, John D. wrote:

the current controller runs even though I get redirected to the
login page. Yes?

Thanks again for your valuable feedback!

Yup, you got it!

The controller is initialized with the request and response objects.

Then it processes the request, which begins with the before_filters,
then calls the appropriate action and renders the view, then calls
the after_filters.

So initialize is outside of the request processing chain.

Best,
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDeRnyAQHALep9HFYRAue4AKCy8IHj+GmK0lRBaUakEIeYeispngCaA5ey
QbjB6E0mtoDKzFPev5h5d58=
=zisb
-----END PGP SIGNATURE-----


#8

On 11/15/05, Jeremy K. removed_email_address@domain.invalid wrote:

found)? Anybody?

The controller is initialized with the request and response objects.

Then it processes the request, which begins with the before_filters,
then calls the appropriate action and renders the view, then calls
the after_filters.

So initialize is outside of the request processing chain.

Best,
jeremy

I have a related problem. When I try to access @params or params in a
before filter, I get a nil object error. I do not have an initialize
method.

  • Rowan

#9

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 14, 2005, at 2:20 PM, John D. wrote:

So, the bigger question for me now is why would @params, @request,
@session not be available in initialize (as you found)? Anybody?

The initialize method is used to … initialize the controller. It’s
where these instance variables are created for your use. Use
before_filters rather than override it.

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDeRFKAQHALep9HFYRAtwzAKCNkR1DYFXUWJuCtR27Wk5dypcgaQCfdGHN
1V5e7heSCGwbIsvyi7mGBN8=
=4fHh
-----END PGP SIGNATURE-----