# Need help with sessions

**URL:** <https://www.ruby-forum.com/t/need-help-with-sessions/113559>\
**Category:** Rails\
**Created:** [September 1, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/need-help-with-sessions/113559 "2007-09-01T19:23:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shybe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@shybe](https://www.ruby-forum.com/u/shybe)\
**Post date:** [September 1, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/need-help-with-sessions/113559/1 "2007-09-01T19:23:33Z")

</div>

Ok, I need my users to fill in form hit a “continue” button, then be  
taken to a page were they can preview the content they just entered in  
the previous form. I know I need sessions and cookies to make this  
happen, but I’m not sure where to store the session data, or how to  
retrieve it. Can someone please explain this to me, as I am new to ROR  
and programming in general?

---

<div class="post-metadata">

**Author:** ![shybe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@shybe](https://www.ruby-forum.com/u/shybe)\
**Post date:** [September 1, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/need-help-with-sessions/113559/2 "2007-09-01T19:35:35Z")

</div>

[http://www.google.com/search?q=rails+session](http://www.google.com/search?q=rails+session)

Rein

---

<div class="post-metadata">

**Author:** ![shybe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@shybe](https://www.ruby-forum.com/u/shybe)\
**Post date:** [September 1, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/need-help-with-sessions/113559/3 "2007-09-01T19:57:37Z")

</div>

Rein,

Thanks for the link, I dont know maybe its just not clicking for me,  
but where I get confused is in the syntax.  
For example: if I have a form with name, email, and message, fields,  
then I I create a method…

def preview  
session[:name] = ?  
session[:email] = ?  
session[:message] =?  
end

I’m not sure what to put after the = sign or if maybe I’m not putting  
the correct thing in the braces, or maybe I’m missing some step all  
together.

I think I’m missing something very basic, so any help would be greatly  
appreciated, I think I need the dummy explanantion. Thanks.

---

<div class="post-metadata">

**Author:** ![shybe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@shybe](https://www.ruby-forum.com/u/shybe)\
**Post date:** [September 1, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/need-help-with-sessions/113559/4 "2007-09-01T20:20:40Z")

</div>

Here’s the short of it:

session is a hash. Rails (magically) persists this hash for you from  
request to request, but all you need to know for now is that it’s a  
hash and that you set and get keys in the session hash the same as you  
do in any other hash. So as an example:

session[:foo] = “bar”  
session[:foo] #=\> “bar” ( session[:foo] returns the string “bar” )

since session is just a hash, you can play around in irb by doing

session = Hash.new # or just session = {}

to get a feel for how it works.

Hashes can use anything as the key and store anything as the value,  
but convention is to use symbols ( :something ) as your session keys  
so just do that 🙂 This is also a good practice for hashes in general.

It’s fine to store a string or any other simple (small) object in  
session (unless it’s a 40MB long string or something):

session[:my\_string] = “My string”  
session[:my\_hash] = { :foo =\> “bar”, :bizz =\> "bazz }

but convention is to store references to complex (big) objects (like  
an ActiveRecord object) rather than the objects themselves, so for  
instance:

session[:user\_id] = @user.id

is better than

session[:user] = @user

Follow that one too. It’s a Good Thing. 🙂

Good luck

Rein
