Forum: Ruby on Rails session weirdness

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.
jdell (Guest)
on 2005-11-14 23:50
(Received via mailing list)
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 :-(

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

Thanks!
John
cool_screen_name90001 (Guest)
on 2005-11-15 00:11
(Received via mailing list)
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.
http://farechase.yahoo.com
jdell (Guest)
on 2005-11-15 00:20
(Received via mailing list)
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
jeremy (Guest)
on 2005-11-15 00:38
(Received via mailing list)
-----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-----
alastair (Guest)
on 2005-11-15 00:53
(Received via mailing list)
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
jeremy (Guest)
on 2005-11-15 00:56
(Received via mailing list)
-----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-----
jdell (Guest)
on 2005-11-15 01:02
(Received via mailing list)
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
jeremy (Guest)
on 2005-11-15 01:14
(Received via mailing list)
-----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-----
bigsmoke (Guest)
on 2005-11-24 12:59
(Received via mailing list)
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
This topic is locked and can not be replied to.