# Session WTF?

**URL:** <https://www.ruby-forum.com/t/session-wtf/108293>\
**Category:** Rails\
**Created:** [July 1, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/session-wtf/108293 "2007-07-01T21:59:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CptPingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@CptPingu](https://www.ruby-forum.com/u/CptPingu)\
**Post date:** [July 1, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/session-wtf/108293/1 "2007-07-01T21:59:56Z")

</div>

Hi.

How to use session in ruby on rails ?  
I try to make it work for hours, but it still doesn’t work…

In my controller I have this:

def identification  
@current\_user = User.new(params[:user])  
ok = false  
redir = ‘login\_failed’  
@remote\_user = User.find(:all)  
@remote\_user.each do |r\_user|  
if (r\_user.login == @current\_user.login and  
r\_user.pass == @current\_user.pass)  
ok = true  
end  
end  
if (ok)  
session[:user\_login] = params[:login]  
session[:user\_access] = params[:access]  
redir = ‘list\_article’  
end  
redirect\_to :action =\> redir  
end

I want to display the name of logged person.  
In my views I have:

\<%= if session[:user\_login] then  
“OK #{session[:user\_login]}”  
else  
“Php RoX !”  
end %\>

But “session[:user\_login]” is always empty.

Thanks.

---

<div class="post-metadata">

**Author:** ![CptPingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@CptPingu](https://www.ruby-forum.com/u/CptPingu)\
**Post date:** [July 2, 2007, 5:57am UTC](https://www.ruby-forum.com/t/session-wtf/108293/2 "2007-07-02T05:57:46Z")

</div>

CptPingu wrote:

> How to use session in ruby on rails ?  
> I try to make it work for hours, but it still doesn’t work…

You’re doing it basically right: if you set attributes of session, you  
should be able to read them back.

Your code to set the session variables looks very confused though. I’d  
bet that there’s at least one major bug in your identification method.  
You should try logging the contents of the variables at various points  
in it and use that to work out what’s going on.

- Pete

---

<div class="post-metadata">

**Author:** ![CptPingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@CptPingu](https://www.ruby-forum.com/u/CptPingu)\
**Post date:** [July 2, 2007, 9:44am UTC](https://www.ruby-forum.com/t/session-wtf/108293/3 "2007-07-02T09:44:58Z")

</div>

this piece of code:

```
@remote_user = User.find(:all)
@remote_user.each do |r_user|
  if (r_user.login == @current_user.login and
      r_user.pass == @current_user.pass)
    ok = true
  end

```

you’re iterating over an array of all of the users in the table, and you  
set the ‘ok’ variable to true, if one of them matches. this is great,  
but the next loop in the iteration will set ‘ok’ back to false, since  
i’m guessing we’re not talking about a universe where all of the users  
are a duplicate of the same person.  
mmm.

if you add in a

```
  ok = true
     return

```

it should work.  
else  
“Php RoX !” (not)

---

<div class="post-metadata">

**Author:** ![CptPingu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@CptPingu](https://www.ruby-forum.com/u/CptPingu)\
**Post date:** [July 2, 2007, 11:25am UTC](https://www.ruby-forum.com/t/session-wtf/108293/4 "2007-07-02T11:25:05Z")

</div>

Better way to do the same:

def identification  
if User.find(:conditions =\> [“login = ? and pass =  
?”,params[:user][:login],params[:user][:pass]])  
session[:user\_login] = params[:login]  
session[:user\_access] = params[:access]  
redirect\_to :action =\> ‘list\_article’  
else  
redirect\_to :action =\> ‘login\_failed’  
end  
end

> def identification  
> @current\_user = User.new(params[:user])  
> ok = false  
> redir = ‘login\_failed’  
> @remote\_user = User.find(:all)  
> @remote\_user.each do |r\_user|  
> if (r\_user.login == @current\_user.login and  
> r\_user.pass == @current\_user.pass)  
> ok = true  
> end  
> end  
> if (ok)  
> session[:user\_login] = params[:login]  
> session[:user\_access] = params[:access]  
> redir = ‘list\_article’  
> end  
> redirect\_to :action =\> redir  
> end
> 
> But “session[:user\_login]” is always empty.
