Need help with sessions

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?

http://www.google.com/search?q=rails+session

Rein

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.

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 :slight_smile: 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. :slight_smile:

Good luck

Rein

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs