# Help with state in CGI with Ruby

**URL:** <https://rubytalk.org/t/help-with-state-in-cgi-with-ruby/5665>\
**Category:** ruby-talk\
**Created:** [16 April 2003 14:08 UTC](https://rubytalk.org/t/help-with-state-in-cgi-with-ruby/5665 "2003-04-16T14:08:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brett\_H\_Williams](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Brett\_H\_Williams](https://rubytalk.org/u/Brett_H_Williams)\
**Post date:** [16 April 2003 14:08 UTC](https://rubytalk.org/t/help-with-state-in-cgi-with-ruby/5665/1 "2003-04-16T14:08:08Z")

</div>

A few weeks back I helped write a small web application for some reporting  
purposes using mod\_ruby/eruby. Being completely ignorant of CGI programming,  
it was a fun learning experience, and thanks to those on this list that helped  
me.

Being well received, now a more complicated application (with some  
similarities) has been requested, and this time I’d like it to be less ad hoc.

What I’m looking for is a Ruby package to help with the bookkeeping due to the  
stateless nature of CGI programming.

The previous project used hidden HTML fields to keep state as the client would  
fill in various form pages. The contents of the forms are dependent on  
previous choices, hence the need to maintain state.

It really felt like a bad hack, but a colleague (who did most of the rhtml)  
says that this is just how its done: the parts of the program that interact  
directly with the web are just generally ugly.

There are several ideas that come to mind to help. Simply wrap some methods  
around creating hidden fields to manage data, or use a session ID and the  
Marshal module to write the state to disk. Is there a framework that will help  
with this (and likely other related tasks)?

However my real question is: does something already exist that helps me with  
this? I’ve looked through the RAA, and the closest thing I saw was borges, but  
my platform is Apache (not WEBrick).

Realize I’m not very well versed in the proper terminology etc. so I may be  
missing some obvious things. It may be that what I want is simple enough that  
no one has made a package, if so I guess we’ll just write it. But I just have  
this feeling that something this common has got to be part of a larger  
framework.

Does anyone have any suggestions?

Thanks in advance,

> **···**
>
> –  
> ---------------------------------------------- | --------------------------  
> Brett Williams |
> 
> | Agilent Technologies | [brett\_williams@agilent.com](mailto:brett_williams@agilent.com) |
> | --- | --- |

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard1](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Ara.T.Howard1](https://rubytalk.org/u/Ara.T.Howard1)\
**Post date:** [16 April 2003 17:34 UTC](https://rubytalk.org/t/help-with-state-in-cgi-with-ruby/5665/2 "2003-04-16T17:34:14Z")

</div>

\> It really felt like a bad hack, but a colleague (who did most of the rhtml) \> says that this is just how its done: the parts of the program that interact \> directly with the web are just generally ugly. \> \> There are several ideas that come to mind to help. Simply wrap some methods \> around creating hidden fields to manage data, or use a session ID and the \> Marshal module to write the state to disk. Is there a framework that will \> help with this (and likely other related tasks)? \> \> However my real question is: does something already exist that helps me \> with this? I've looked through the RAA, and the closest thing I saw was \> borges, but my platform is Apache (not WEBrick). \> \> Realize I'm not very well versed in the proper terminology etc. so I may be \> missing some obvious things. It may be that what I want is simple enough \> that no one has made a package, if so I guess we'll just write it. But I \> just have this feeling that something this common has got to be part of a \> larger framework. \> \> Does anyone have any suggestions?

this is super simple, and works great :

[http://raa.ruby-lang.org/list.rhtml?name=cgi-sess-pstore](http://raa.ruby-lang.org/list.rhtml?name=cgi-sess-pstore)

it allows one to store arbitrary objects using cgi sessions. sessions are  
alot slicker than hidden fields, espcecially if you want to store more than  
just strings - or data which is large - between page hits.

the session itself uses arbitrary objects in the way a hash does :

key\_object → value\_object

usages is as in

require ‘cgi’  
require ‘cgi/session/pstore’

cgi = CGI.new  
sessionn = CGI::Session.new(cgi, ‘database\_manager’ =\> CGI::Session::PStore)

key = String.new('some\_key\_object)  
value = [‘can’, ‘be’, ‘most’, ‘anything’]

session[key] = value

# cgi stuff

session.update

you can download it from

[http://raa.ruby-lang.org/list.rhtml?name=cgi-sess-pstore](http://raa.ruby-lang.org/list.rhtml?name=cgi-sess-pstore)

see an example program at

[http://eli.fsl.noaa.gov/ruby/class/0/cgi/session/pstore.cgi](http://eli.fsl.noaa.gov/ruby/class/0/cgi/session/pstore.cgi)

it’s an anoying little program which caches the current time as a Time object  
and displays it every few seconds using meta-refresh - which is terrible but  
illustrates the point. 😉

there’s a link there to view the source.

feel free to contact me with questions.

-a

> **···**
>
> On Wed, 16 Apr 2003, Brett H. Williams wrote:
> 
> # –
> 
> > Ara Howard  
> > NOAA Forecast Systems Laboratory  
> > Information and Technology Services  
> > Data Systems Group  
> > R/FST 325 Broadway  
> > Boulder, CO 80305-3328  
> > Email: [ara.t.howard@fsl.noaa.gov](mailto:ara.t.howard@fsl.noaa.gov)  
> > Phone: 303-497-7238  
> > Fax: 303-497-7259  
> > ====================================
