# Session problem

**URL:** <https://www.ruby-forum.com/t/session-problem/139056>\
**Category:** Rails\
**Created:** [May 30, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/session-problem/139056 "2008-05-30T15:05:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blaede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@Blaede](https://www.ruby-forum.com/u/Blaede)\
**Post date:** [May 30, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/session-problem/139056/1 "2008-05-30T15:05:59Z")

</div>

Hello everyone,

I am just starting a project for school in RoR and I am a complete  
n00b ^^  
Here’s my problem :

I need to get the user’s login stored in the session but for some  
reason I cannot.  
Here’s my login method code :

def login  
if request.post?  
@user = User.find\_by\_username(params[:login])  
if @user and @user.password\_is? params[:password]  
session[:user] = @user.id

```
       redirect_to :controller => 'games'

```

else  
@auth\_error = ‘Wrong username or password’  
end  
end  
end

And here’s the code of my GamesController. User needs to be logged in  
to access the games webpage (using check\_auth method) and on that  
page the user can create a new game (adding a record in the game  
table,  
with a few fields from the form filled by the user and a field with  
the user’s  
username, that’s why I need to get it from session[:user]).

before\_filter :check\_auth

def check\_auth  
unless session[:user]  
flash[:error] = ‘You need to be logged in to access this panel’  
redirect\_to :controller =\> ‘users’, :action =\> ‘login’  
end  
end

def create  
@game = Game.new(params[:game])  
@user = User.find(session[:user])  
@game.host = @user.username

```
respond_to do |format|
  if @game.save
    format.html { redirect_to(@game) }
    format.xml { render :xml => @game, :status

```

=\> :created, :location =\> @game }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @game.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

Thank you very much in advance for any help you could give,  
I really need to get this working to finish my project ^^

Blaede from Belgium

---

<div class="post-metadata">

**Author:** ![Blaede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@Blaede](https://www.ruby-forum.com/u/Blaede)\
**Post date:** [May 30, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/session-problem/139056/2 "2008-05-30T15:44:27Z")

</div>

To understand how to require an user to log in to complete certain  
tasks, I would recomment you to read this excellent tutorial:

[http://www.railsforum.com/viewtopic.php?id=14216](http://www.railsforum.com/viewtopic.php?id=14216)

Regards,

Rey9999

---

<div class="post-metadata">

**Author:** ![Blaede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@Blaede](https://www.ruby-forum.com/u/Blaede)\
**Post date:** [May 30, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/session-problem/139056/3 "2008-05-30T18:14:09Z")

</div>

Thank you very much for your answer Rey.  
I took a look at the tutorial and it seems  
very interesting indeed.

However, my application is very simple,  
all pages are accessible by all, except one  
(the games page) accessible by logged users  
only.

This feature works as it is on my website,  
my problem comes from the fact I have troubles  
extracting information from the session[:user].  
I need to access the fields of the logged @user.

My project must be finished by the 5th of June  
so if someone could help me solve this problem  
without re-designing my entire application,  
I’d be really gratefull.

Thanks again

---

<div class="post-metadata">

**Author:** ![Blaede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@Blaede](https://www.ruby-forum.com/u/Blaede)\
**Post date:** [May 30, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/session-problem/139056/4 "2008-05-30T18:36:33Z")

</div>

On May 30, 5:13 pm, Blaede [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This feature works as it is on my website,  
> my problem comes from the fact I have troubles  
> extracting information from the session[:user].  
> I need to access the fields of the logged @user.

You haven’t actually said what exactly goes wrong.

Fred

---

<div class="post-metadata">

**Author:** ![Blaede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@Blaede](https://www.ruby-forum.com/u/Blaede)\
**Post date:** [May 30, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/session-problem/139056/5 "2008-05-30T19:56:11Z")

</div>

> You haven’t actually said what exactly goes wrong.
> 
> Fred

[QUOTE]I need to get the user’s login stored in the session but for  
some  
reason I cannot. [/QUOTE]

On one hand I have : session[:user] = @user.id  
and on the other hand (in another controller) I have : @user =  
User.find(session[:user])  
which if I am not mistaken should put in @user the session’s user.  
But when in the next line I try to do : game.host = @user.username  
(game has a field host containing a string) and then try to do a  
game.save,  
rails tells me the host field cannot be null…

---

<div class="post-metadata">

**Author:** ![Blaede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@Blaede](https://www.ruby-forum.com/u/Blaede)\
**Post date:** [May 30, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/session-problem/139056/6 "2008-05-30T20:01:11Z")

</div>

Blaede wrote:

> > You haven’t actually said what exactly goes wrong.
> > 
> > Fred
> 
> [QUOTE]I need to get the user’s login stored in the session but for  
> some  
> reason I cannot. [/QUOTE]

that told us only, that “something” went wrong 😉

> On one hand I have : session[:user] = @user.id  
> and on the other hand (in another controller) I have : @user =  
> User.find(session[:user])  
> which if I am not mistaken should put in @user the session’s user.  
> But when in the next line I try to do : game.host = @user.username  
> (game has a field host containing a string) and then try to do a  
> game.save,  
> rails tells me the host field cannot be null…

better that.  
if session[:user] would be empty, then the find would have told you,  
that it can’t find a user without an id. so that id is stored in the  
session.

so @user.username is nil or empty or doesn’t exist. maybe misspelled?

---

<div class="post-metadata">

**Author:** ![Blaede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@Blaede](https://www.ruby-forum.com/u/Blaede)\
**Post date:** [May 30, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/session-problem/139056/7 "2008-05-30T20:36:10Z")

</div>

Hi Blaede,

Blaede wrote:

> On one hand I have : session[:user] = @user.id  
> and on the other hand (in another controller)  
> I have : @user = User.find(session[:user])  
> which if I am not mistaken should put in  
> @user the session’s user.

It will put the object retrieved in your User.find into @user. Problem  
is  
the User.find is returning nil. So you need to figure out why that is.  
A  
three things come to mind right off the bat.

1. It could be that session[:user] is nil because @user.id is nil when  
you  
set session[:user]. Do a puts right before you set session[:user] to  
check  
the value of @user.id in the console, and do likewise afterwards to see  
the  
value of session[:user]
2. It could be that session[:user] is not an int, which is what  
User.find()  
expects. You can check that with session[:user].is\_kind\_of?(int) (check  
the  
syntax) or you can just force it with User.find(session[:user].to\_int)
3. It could be that you’re invoking the controller/method that uses  
session[:user] before the one that sets it.

Use puts to display the values as your app executes and you’ll figure  
out  
which it is, or what else it is if it’s not one of these.

HTH,  
Bill
