Tuesday, December 8, 2009

Fooling Around With Sessions

A very trivial try,


def session_string
if session[:storage].nil?
session[:storage] = "Hi! i reside in the session storage."
else
session[:storage]
end

This action checks whether the session[:storage] is nil? If it is, then just put a string into it. If it isn't then the action will just return the session.


puts session[:storage]

It will display the string stored in the session.

A lil part i picked, from the Agile Development Rails book,


def find_cart
unless(session[:cart])
session[:cart] = Cart.new
end
session[:cart]
end


This action will check if a session[:cart] is nil or contains a cart. If it doesn't then create a new Cart object and store it into the session. If there is, then it will just return the session[:cart] which contains a former Cart object in it.

Session is stored in a cookie by default in Rails. So each browser will have its own session storage and will interact differently between different clients.

0 comments:

Post a Comment