Forum: Ruby CGI question

Posted by Assaf Shomer (assafshomer)
on 2012-11-28 11:58
Ruby Beginner question.

In the "Ruby and the Web" section of the pickaxe book they talk about
CGI and give the following example:

========
require 'cgi'
cgi = CGI.new("html3") # add HTML generation methods
cgi.out do
  cgi.html do
    cgi.head { "\n"+cgi.title { "This Is a Test"} } +
    cgi.body do "\n"+
      cgi.form do"\n"+
      cgi.hr +
      cgi.h1 { "A Form: " } + "\n"+
      cgi.textarea("get_text") +"\n"+
      cgi.br +
      cgi.submit
      end
    end
  end
end
=============

They claim that this program produces:
==============
Content-Type: text/html
Content-Length: 302
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD>
<TITLE>This Is a Test</TITLE></HEAD><BODY>
<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">
<HR><H1>A Form: </H1>
<TEXTAREA NAME="get_text" COLS="70" ROWS="10"></TEXTAREA>
<BR><INPUT TYPE="submit"></FORM></BODY></HTML>
======

When i run this program i get nothing on the console output, and when i
tried to run it in irb, it got stuck on the cgi = CGI.new("html3") line
(by stuck I mean further ENTERs don't give me the standard
irb(main):007:0> ) and gave the following message:
==============
(offline mode: enter name=value pairs on standard input)
==============
can anyone explain to me what's going on?

Thanks.

Assaf.
Posted by Jan E. (jacques1)
on 2012-11-28 12:18
Hi,

CGI scripts are executed by a webserver like Apache. It makes no sense
to run them in the console.

By the way, this code looks ancient. HTML 3.2 is 15(!) years old, it was 
around when the internet was still young.
Posted by Carlo E. Prelz (Guest)
on 2012-11-28 12:29
(Received via mailing list)
Subject: CGI question
  Date: Wed 28 Nov 12 07:58:17PM +0900

Quoting Assaf Shomer (lists@ruby-forum.com):

> When i run this program i get nothing on the console output, and when i
> tried to run it in irb, it got stuck on the cgi = CGI.new("html3") line
> (by stuck I mean further ENTERs don't give me the standard
> irb(main):007:0> ) and gave the following message:
> ==============
> (offline mode: enter name=value pairs on standard input)
> ==============
> can anyone explain to me what's going on?

I have no experience with the cgi package at all, but the message is
clear: IRB is waiting for something on its standard input, waiting for
name=value pairs. If you want to find out exactly what these
name=value pairs are, I suggest you study the cgi package more. But I
can tell you that (at least in Linux) you tell the shell (and IRB)
that you are done with providing data from the standard input with
Ctrl-D. Indeed, if you press Ctrl-D at the above prompt, the object
creation call returns successfully.

Carlo
Posted by Prasadhnc C (Guest)
on 2012-11-28 12:40
(Received via mailing list)
entering control-z will print the html contents on the console,
If you want to pass some input to the script, you can enter name-value
pairs in the console
like, name=xyx.
Posted by Ryan Davis (Guest)
on 2012-11-28 23:25
(Received via mailing list)
On Nov 28, 2012, at 03:18 , Jan E. <lists@ruby-forum.com> wrote:

> CGI scripts are executed by a webserver like Apache. It makes no sense
> to run them in the console.

not true:

10001 % ruby19
require 'cgi'
cgi = CGI.new("html3") # add HTML generation methods
cgi.out do
 cgi.html do
   cgi.head { "\n"+cgi.title { "This Is a Test"} } +
   cgi.body do "\n"+
     cgi.form do"\n"+
     cgi.hr +
     cgi.h1 { "A Form: " } + "\n"+
     cgi.textarea("get_text") +"\n"+
     cgi.br +
     cgi.submit
     end
   end
 end
end
^d
(offline mode: enter name=value pairs on standard input)
^d
Content-Type: text/html
Content-Length: 302

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD>
<TITLE>This Is a Test</TITLE></HEAD><BODY>
<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">
<HR><H1>A Form: </H1>
<TEXTAREA NAME="get_text" COLS="70" ROWS="10"></TEXTAREA>
<BR><INPUT TYPE="submit"></FORM></BODY></HTML>
Posted by Jan E. (jacques1)
on 2012-11-29 01:04
Ryan Davis wrote in post #1086967:
> On Nov 28, 2012, at 03:18 , Jan E. <lists@ruby-forum.com> wrote:
>
>> CGI scripts are executed by a webserver like Apache. It makes no sense
>> to run them in the console.
>
> not true:

I did not mean it's impossible. Of course CGI scripts are normal 
programs.

But I don't see the point of writing CGI scripts without actually 
running them on a webserver and inspecting the result in a browser. What 
do you learn from an obscure script that you have to execute in a 
"strange" way to see some random gibberish on the console? There isn't 
any context.

Anyway, if that was the goal of the exercise, I guess he can move on 
now.
Posted by Eliezer Croitoru (Guest)
on 2012-11-29 06:01
(Received via mailing list)
Hey Assaf,

In a real server case it would send parameters to the script.
In a command line case once the cgi code scope ended it will require the
parameters entered one by one and CTRL+D should run it.

I am using ruby for CGI scripts but formatting a form this way can be
very painful.
I would go for writing the RAW html by myself with touches of variables
if needed.

Regards,
Eliezer
Posted by Assaf Shomer (assafshomer)
on 2012-11-29 13:35
Thanks Ryan, That definitely did the trick.
I had no idea that i can just type 'ruby1.9.1' and then type ruby code
and CTRL+D executes it.

Actually, it also works from inside eclipse. cool. live and learn.

Thanks again,

Assaf.

---------------------------------------------

Ryan Davis wrote in post #1086967:
> On Nov 28, 2012, at 03:18 , Jan E. <lists@ruby-forum.com> wrote:
>
>> CGI scripts are executed by a webserver like Apache. It makes no sense
>> to run them in the console.
>
> not true:
>
> 10001 % ruby19
> require 'cgi'
> cgi = CGI.new("html3") # add HTML generation methods
> cgi.out do
>  cgi.html do
>    cgi.head { "\n"+cgi.title { "This Is a Test"} } +
>    cgi.body do "\n"+
>      cgi.form do"\n"+
>      cgi.hr +
>      cgi.h1 { "A Form: " } + "\n"+
>      cgi.textarea("get_text") +"\n"+
>      cgi.br +
>      cgi.submit
>      end
>    end
>  end
> end
> ^d
> (offline mode: enter name=value pairs on standard input)
> ^d
> Content-Type: text/html
> Content-Length: 302
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD>
> <TITLE>This Is a Test</TITLE></HEAD><BODY>
> <FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">
> <HR><H1>A Form: </H1>
> <TEXTAREA NAME="get_text" COLS="70" ROWS="10"></TEXTAREA>
> <BR><INPUT TYPE="submit"></FORM></BODY></HTML>
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.