Ruby CGI Programming

Ruby is a general-purpose language, not just a language for web development, but Ruby is the most common development in web applications and web tools.

With Ruby you can not only write your own SMTP server, FTP program, or Ruby web server, but you can also use C++ for Ruby programming.

Next, let's take a moment to learn Ruby's CGI editor.

Web browsing

To better understand how CGI works, we can click on a link or URL from the web page:

  • 1. Use your browser to access the URL and connect to the HTTP web server.
  • 2. After receiving the request information, the web server will parse the URL and find out whether the accessed file exists on the server. If there is content of the returned file, an error message is returned.
  • 3. The browser receives information from the server and displays the received file or error message.

CGI programs can be Ruby scripts, Python scripts, PERL scripts, SHELL scripts, C or C++ programs, and more.

Web Server Support and Configuration

Before you do CGI programming, make sure your web server supports CGI and CGI handlers.

Apache supports CGI configuration:

Set up the CGI directory:

ScriptAlias /cgi-bin/ var/www/cgi-bin/

All HTTP server execution CGI programs are stored in a pre-configured directory. This directory is called the CGI directory and, by convention, it is named the /var/www/cgi-bin directory.

CGI files have a .cgi extension and Ruby can also use the .rb extension.

By default, the Linux server configuration runs in the cgi-bin directory as /var/www.

If you want to specify a different directory to run CGI scripts, you can modify the httpd.conf configuration file as follows:

<Directory "/ Var/www/cgi-bin">
   AllowOverride None
   Options +ExecCGI
   Order allow,deny
   Allow from all

Add the .rb suffix to the AddHandler so that we can access the Ruby script file ending in .rb:

AddHandler cgi-script .cgi . pl .rb

Write a CGI script

The most basic Ruby CGI code looks like this:


Puts "Content-type: text/html\n\n"
Puts "<html><body>This is a test</body></html>"

You can keep this code in the test.cgi file, and last time you go to the server and give it enough permissions to execute as a CGI script.

If your site's address is http://www.example.com/, you can use http://www.example.com/test.cgi to access the program and output: "This is a test .".

When the browser accesses the URL, the web server finds the test.cgi file in the site directory, then parses the script code and accesses the HTML document through the Ruby parser.

Use cgi.rb

Ruby can call CGI libraries to write more complex CGI scripts.

The following code calls the CGI library to create a scripted CGI script.


require 'cgi'

Cgi = CGI.new
Puts cgi.header
Puts "<html><body>This is a test</body></html>"

In the following code, a CGI object is created and the header information is printed.

Form Processing

The CGI library can be used to get data for form submissions (or parameters in URLs) in two ways. For example, the URL: /cgi-bin/test.cgi?FirstName=Zara&LastName=Ali.

You can use CGI#[] to get the parameters FirstName and LastName directly:


require 'cgi'
Cgi = CGI.new
Cgi['FirstName'] # => ["Zara"]
Cgi['LastName'] # => ["Ali"]

Another way to get form data:

require 'cgi'
Cgi = CGI.new
h = cgi.params # => {"FirstName"=>["Zara"],"LastName"=> ["Ali"]}
h['FirstName'] # => ["Zara"]
h['LastName'] # => ["Ali"]

The following code is used to retrieve all key values:


require 'cgi'
Cgi = CGI.new
Cgi.keys # => ["FirstName", "LastName "]

If the form contains multiple fields of the same name, the values ​​of the same field will be saved in the array.

In the following example, specify the same three fields "name" in the form, with values ​​"Zara", "Huma" and "Nuha":


require 'cgi'
Cgi = CGI.new
Cgi['name'] # => "Zara"
Cgi.params['name'] # => ["Zara" , "Huma", "Nuha"]
Cgi.keys # => ["name"]
Cgi.params # => {"name"=>["Zara", "Huma", "Nuha"]}

Note: Ruby will automatically determine the GET and POST methods, so there is no need to treat the two methods differently.

Here are the relevant HML codes:

<form method="POST" action="http://www.example.com/test.cgi">
First Name :<input type="text" name ="FirstName" value< /span>=""  />
<br />
Last Name :<input type="text" name ="LastName" value< /span>=""  />

<input type="submit" value="Submit Data" />< /span>

Create a Form form and HTML

CGI contains a number of methods to create HTML, and each HTML tag has a corresponding method. Before using these methods, you must create a CGI object by using CGI.new.

In order to make the nesting of tags easier, these methods use the content as a block of code, and the code block will return the string as the content of the tag. As shown below:


require "cgi"
Cgi = CGI.new("html4")
      Cgi.head{ "\n"+cgi.title{"This Is a Test "} } +
      Cgi.body{ "\n"+
            Cgi.hr +
            Cgi.h1 { "A Form: " } + "\n"+
            Cgi.textarea("get_text") +"\n"+
            Cgi.br +

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.