Home »
Ruby programming
CGI programming in Ruby
In this tutorial, we are going to learn about the CGI (Common Gateway Interface) programming in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on September 07, 2019
Ruby CGI programming
Before getting into what CGI programming means in Ruby, let us understand what it means the real programming world. CGI is an abbreviation for Common Gateway Interface. It is identification for conveying data or information between the World Wide Web and a CGI program. CGI programs provide a dynamic way to interact with the user and designed in a way that they can accept and return data as well. All the processing occurs at the Web Server; hence it is used as a Server-Side solution.
Now, let us understand the way to write CGI programs in Ruby.
If you want to generate HTML output from a Ruby script, you can write the following code:
print "HTTP/1.0 200 OK\r\n"
print "Content-type: text/html\r\n\r\n"
print "<html><body>Hello World!</body></html>\r\n"
Output
HTTP/1.0 200 OK
Content-type: text/html
<html><body>Hello fella! Greetings from IncludeHelp.com</body></html>
cgi.rb is a predefined class termed as cgi helps you to write CGI programs. You can create forms, cookies, maintain stateful sessions and many more, with the support of cgi class. You have to give the statement require cgi at the top of the program to enjoy the capabilities of cgi class.
Let us see some of the abilities of CGI class.
Quoting
If are writing a URL or an HTML code, you are supposed to be very careful with characters. Many characters have special meaning in URLs. Translations of some of the characters are given below:
- / => %2F
- & => %26
- <space> => +
To handle the above characters, cgi provides you .escape and .unescape. They are routines of cgi. See the example given below for reference:
require 'cgi'
puts CGI.escape( "He/She must be aware of his/her capabilities & should work accordingly" )
Output
He%2FShe+must+be+aware+of+his%2Fher+capabilities+%26+should+work+accordingly
The following code will let you know how you can escape HTML special characters?
require 'cgi'
puts CGI.escapeHTML( '<a href="/san">Go to the site(Includehelp.com)</a>' )
Output
<a href="/san">Go to the site(Includehelp.com)</a>
Creating forms and HTML
cgi class is an umbrella of the huge number of methods which are necessary to write HTML. Every tag has got a method under cgi. cgi is a class and we all that to access the methods of a class, first we have to instantiate it. cgi can be instantiated by calling CGI.new.
Let us design a basic form with the help of code mentioned below,
require "cgi"
cgi = CGI.new("html5")
cgi.out {
cgi.html {
cgi.head { "\n"+cgi.title{"It is an Example of form"} } +
cgi.body { "\n"+
cgi.form {"\n"+
cgi.hr +
cgi.h1 { "A Form: " } + "\n"+
cgi.h2 { "A Basic Form:"} + "\n" +
cgi.textarea("get_text") +"\n"+
cgi.button("click_here") +"\n"+
cgi.hr +
cgi.br +
cgi.submit
}
}
}
}
Output:
Content-Type: text/html
Content-Length: 317
<!DOCTYPE HTML><HTML><HEAD>
<TITLE>It is an Example of form</TITLE></HEAD><BODY>
<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">
<HR><H1>A Form: </H1>
<H2>A Basic Form:</H2>
<TEXTAREA NAME="get_text" COLS="70" ROWS="10"></TEXTAREA>
<BUTTON></BUTTON>
<HR><BR><INPUT TYPE="submit"></FORM></BODY></HTML>
The above code is producing an HTML form having title "This is an example". The form has horizontal rulers, Headings, two Input controls namely TextArea and button. All the code blocks will return a string which will be utilized as the content for the tag.