Ruby CGI Programming

Ruby is a broadly useful language, a language for web improvement, yet Ruby is the most widely recognized advancement in web applications and web instruments.

With Ruby you can not just compose your very own SMTP server, FTP program, or Ruby web server, yet you can likewise utilize C++ for Ruby programming.

Next, how about we pause for a minute to become familiar with Ruby's CGI proofreader.

Web browsing

To better see how CGI functions, we can tap on a connection or URL from the web page:

  • 1. Utilize your program to get to the URL and interface with the HTTP web server.
  • 2. In the wake of accepting the solicitation data, the web server will parse the URL and see if the got to document exists on the server. In the event that there is substance of the returned document, a mistake message is returned.
  • 3. The program gets data from the server and showcases the got document or mistake message.

CGI projects can be Ruby contents, Python contents, PERL contents, SHELL contents, C or C++ projects, and that's only the tip of the iceberg.

CGI engineering outline


Web Server Support and Configuration

Before you do CGI programming, ensure your web server bolsters CGI and CGI handlers.

Apache bolsters CGI configuration:

Set up the CGI directory:

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

All HTTP server execution CGI programs are put away in a pre-arranged registry. This index is known as the CGI registry and, by tradition, it is named the/var/www/cgi-receptacle catalog.

CGI records have a .cgi expansion and Ruby can likewise utilize the .rb augmentation.

As a matter of course, the Linux server setup keeps running in the cgi-receptacle catalog as/var/www.

In the event that you need to indicate an alternate registry to run CGI contents, you can change the httpd.conf setup record as follows:

<Directory "/Var/www/cgi-bin"> 

AllowOverride None 

Choices +ExecCGI 

Request allow,deny 

Permit from all 


Add the .rb postfix to the AddHandler with the goal that we can get to the Ruby content document finishing off with .rb:

AddHandler cgi-script .cgi . pl .rb

Write a CGI script

The most fundamental Ruby CGI code looks like this:


Puts "Content-type: content/html\n\n" 

Puts "<html><body>This is a test</body></html>"

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

If your site's location is http://www.example.com/, you can utilize http://www.example.com/test.cgi to get to the program and yield: "This is a test .".

When the program gets to the URL, the web server finds the test.cgi record in the webpage registry, at that point parses the content code and gets to the HTML report through the Ruby parser.

Use cgi.rb

Ruby can call CGI libraries to compose progressively complex CGI contents.

The following code calls the CGI library to make a scripted CGI content.


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 naturally decide the GET and POST techniques, so there is no compelling reason to treat the two strategies in an unexpected way.

Here are the important HML codes:



<form method="POST" action="http://www.example.com/test.cgi"> 

First Name :<input type="text" name ="FirstName" value="" /> 

<br /> 

Last Name :<input type="text" name ="LastName" value="" /> 

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




Create a Form structure and HTML

CGI contains various techniques to make HTML, and every HTML tag has a comparing strategy. Prior to utilizing these strategies, you should make a CGI object by utilizing CGI.new.

In request to make the settling of labels simpler, these strategies utilize the substance as a square of code, and the code square will restore the string as the substance of the tag. As appeared/p>


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 +

String escaping

When you are working with parameters in a URL or HTML form data, you need to escape the specified special characters, such as: quotation marks ("), backslashes (/).

Ruby CGI objects provide CGI.escape and CGI.unescape methods to handle the escaping of these special characters:


require 'cgi'
Puts CGI.escape(Zara Ali/A Sweet & Sour Girl")

The above code execution results are as follows:


require 'cgi'
Puts CGI.escape(Zara Ali/A Sweet & Sour Girl")

Another set of examples:


require 'cgi'
Puts CGI.escapeHTML('<h1>Zara Ali/A Sweet & Sour Girl</h1>')

The above code execution results are as follows:

&lt;h1&gt;Zara Ali/A Sweet & Sour Girl&lt;/ h1&gt;'

Common methods in CGI classes

The following are related methods for complete CGI classes in Ruby

  • - Standard CGI library related methods

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