# CGI and re-direction

**URL:** <https://www.ruby-forum.com/t/cgi-and-re-direction/104348>\
**Category:** Ruby\
**Created:** [May 16, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/cgi-and-re-direction/104348 "2007-05-16T23:57:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 16, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/cgi-and-re-direction/104348/1 "2007-05-16T23:57:37Z")

</div>

I have written a simple CGI script. It loads in an object, and prints  
out a few of its attributes. I would like to add some check-boxes such  
that when I hit the submit button some other booleans get set, it  
accesses a second CGI page, which updates the object based on the  
cgi.params, and then returns to the first page so that the object can be  
be re-displayed (hope that all makes sense - it’s getting late!)

So my question is, how do I redirect back to the first page from the  
second? Or is there another (better) way to achieve the same result  
(perhaps by using just the one page)

Thanks!

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 17, 2007, 12:56am UTC](https://www.ruby-forum.com/t/cgi-and-re-direction/104348/2 "2007-05-17T00:56:34Z")

</div>

> So my question is, how do I redirect back to the first page  
> from the second? Or is there another (better) way to achieve  
> the same result (perhaps by using just the one page)

Just post to the same script, and have it check whether it has been  
posted to, and, optionally, an action parameter (in case a previous page  
posts to it) to tell it which action needs to be taken.

-Doug

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 17, 2007, 11:19am UTC](https://www.ruby-forum.com/t/cgi-and-re-direction/104348/3 "2007-05-17T11:19:25Z")

</div>

Doug P. wrote:

> > So my question is, how do I redirect back to the first page  
> > from the second? Or is there another (better) way to achieve  
> > the same result (perhaps by using just the one page)
> 
> Just post to the same script, and have it check whether it has been  
> posted to, and, optionally, an action parameter (in case a previous page  
> posts to it) to tell it which action needs to be taken.
> 
> -Doug

Thanks for the quick reply. If I use the same script don’t I have  
problems using cgi.params, as on the first visit they don’t exist? Or  
do I use an if…then to choose between two pages (one used pre-posting  
and the other post-posting(!)) As you can tell, I’m not a programmer per  
se, so any examples (or pointers to examples) would be very much  
appreciated.

thanks again

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 17, 2007, 11:22am UTC](https://www.ruby-forum.com/t/cgi-and-re-direction/104348/4 "2007-05-17T11:22:51Z")

</div>

[Clarification of previous post]  
when I said “do I use an if…then to choose between two pages (one  
used pre-posting and the other post-posting” what I meant was:

#!/bin/ruby  
…

if FIRST\_VISIT # (how do I test?)  
cgi.out { a page not referring to cgi.params}  
else  
cgi.out { a page using cgi.params }  
end

Or can I avoid this? Thanks!
