# Session weirdness

**URL:** <https://www.ruby-forum.com/t/session-weirdness/47892>\
**Category:** Rails\
**Created:** [November 14, 2005, 10:50pm UTC](https://www.ruby-forum.com/t/session-weirdness/47892 "2005-11-14T22:50:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 14, 2005, 10:50pm UTC](https://www.ruby-forum.com/t/session-weirdness/47892/1 "2005-11-14T22:50:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 14, 2005, 11:20pm UTC](https://www.ruby-forum.com/t/session-weirdness/47892/2 "2005-11-14T23:20:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 14, 2005, 11:11pm UTC](https://www.ruby-forum.com/t/session-weirdness/47892/3 "2005-11-14T23:11:32Z")

</div>

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](mailto: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](http://lists.rubyonrails.org/mailman/listinfo/rails)

craig

* * *

Yahoo! FareChase: Search multiple travel sites in one click.  
[http://farechase.yahoo.com](http://farechase.yahoo.com)

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 14, 2005, 11:53pm UTC](https://www.ruby-forum.com/t/session-weirdness/47892/4 "2005-11-14T23:53:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 14, 2005, 11:56pm UTC](https://www.ruby-forum.com/t/session-weirdness/47892/5 "2005-11-14T23:56:39Z")

</div>

-----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-----

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 15, 2005, 12:02am UTC](https://www.ruby-forum.com/t/session-weirdness/47892/6 "2005-11-15T00:02:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 15, 2005, 12:14am UTC](https://www.ruby-forum.com/t/session-weirdness/47892/7 "2005-11-15T00:14:43Z")

</div>

-----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-----

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 24, 2005, 11:59am UTC](https://www.ruby-forum.com/t/session-weirdness/47892/8 "2005-11-24T11:59:32Z")

</div>

On 11/15/05, Jeremy K. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![jdell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c57346/32.png) [@jdell](https://www.ruby-forum.com/u/jdell)\
**Post date:** [November 14, 2005, 11:38pm UTC](https://www.ruby-forum.com/t/session-weirdness/47892/9 "2005-11-14T23:38:36Z")

</div>

-----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-----
