The best Ruby CGI programming Tutorial In 2024, In this tutorial you can learn Web browsing,CGI Chart,Web server configuration and support,Write CGI scripts,Use cgi.rb,Forms Processing,Creating Forms and HTML Form,String escape,CGI class methods commonly used,Cookies and Sessions,
Ruby is a universal language, not just the one used in WEB development language, but Ruby development in WEB applications and WEB tools are the most common.
Not only can you use Ruby to write your own SMTP server, FTP program, or Ruby Web server, but also can be use Ruby CGI programming.
Let's take a moment to learn the Ruby CGI edited.
To better understand how CGI works, we can click on a link on a web page or URL of the process:
CGI programs can be a Ruby script, Python script, PERL script, SHELL script, C or C ++ programs.
Before you conduct CGI programming, make sure that your Web server has been configured to support CGI and CGI handler.
Apache supports CGI configuration:
Set up the CGI directory:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
All HTTP server to execute CGI programs are stored in a pre-configured directory. This directory is called the CGI directory, and by convention, it is named / var / www / cgi-bin directory.
CGI file extension .cgi, Ruby can also be used .rb extension.
By default, Linux server configuration running cgi-bin directory is / 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 </Directory>
Add .rb suffix AddHandler, so that we can access .rb end of the Ruby script file:
AddHandler cgi-script .cgi .pl .rb
Basic Ruby CGI code as follows:
#!/usr/bin/ruby puts "Content-type: text/html\n\n" puts "<html><body>This is a test</body></html>"
You can keep the code to test.cgi file server and the last to impart sufficient permissions to perform as a CGI script.
If the address you stand as http://www.example.com/, http://www.example.com/test.cgi used to access the program, the output is: "This is a test.".
The browser to access the Web site, Web server will find test.cgi file in the site directory, and then to resolve the Ruby interpreter and script code by accessing an HTML document.
Ruby can call CGI library to write more complex CGI scripts.
The following code calls the CGI script library to create a CGI script.
#!/usr/bin/ruby require 'cgi' cgi = CGI.new puts cgi.header puts "<html><body>This is a test</body></html>"
The following code, create a CGI object and print header information.
Using CGI library can be obtained in two ways to submit the form (or parameters in the URL) of data, such as URL:? /cgi-bin/test.cgi FirstName = Zara & LastName = Ali.
You can use the CGI # [] directly obtain parameters FirstName and LastName:
#!/usr/bin/ruby require 'cgi' cgi = CGI.new cgi['FirstName'] # => ["Zara"] cgi['LastName'] # => ["Ali"]
Another method for obtaining the form data:
#!/usr/bin/ruby 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 the key:
#!/usr/bin/ruby require 'cgi' cgi = CGI.new cgi.keys # => ["FirstName", "LastName"]
If the form contains multiple fields with the same name, then the value of the same field will be stored in an array.
The following example, specify the form of three identical fields "name", values of "Zara", "Huma" and "Nuha":
#!/usr/bin/ruby 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, there is no need to distinguish between the two methods.
Here is the relevant HML Code:
<html> <body> <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" /> </form> </body> </html>
CGI contains a lot of ways to create HTML, each HTML tag has a corresponding method. Before using these methods, the ratio must CGI to create objects CGI.new.
In order to make the label more simple nesting, these methods will be content as a block of code, the code block returns the string as a content label. As follows:
#!/usr/bin/ruby require "cgi" cgi = CGI.new("html4") cgi.out{ cgi{ cgi.head{ "\n"+cgi.title{"This Is a Test"} } + cgi.body{ "\n"+ cgi.form{"\n"+ cgi.hr + cgi.h1 { "A Form: " } + "\n"+ cgi.textarea("get_text") +"\n"+ cgi.br + cgi.submit } } } }
When you are dealing with parameters in the URL or HTML form data, you need to specify the escape special characters, such as: quotation marks ( "), slash (/).
Ruby CGI object provides CGI.escape CGI.unescape and methods to deal with escaping these special characters:
#!/usr/bin/ruby require 'cgi' puts CGI.escape(Zara Ali/A Sweet & Sour Girl")
The above code is executed as follows:
#!/usr/bin/ruby require 'cgi' puts CGI.escape(Zara Ali/A Sweet & Sour Girl")
Another set of examples:
#!/usr/bin/ruby require 'cgi' puts CGI.escapeHTML('<h1>Zara Ali/A Sweet & Sour Girl</h1>')
The above code is executed as follows:
<h1>Zara Ali/A Sweet & Sour Girl</h1>'
Here are the complete method in Ruby CGI class