A Brief Introduction to CGI programming in Ruby and a brief introduction to rubycgi
Ruby is a common language, not just a language used for WEB development, but Ruby development in WEB applications and WEB tools is the most common.
With Ruby, you can not only write your own SMTP server, FTP program, or Ruby Web server, but also use Ruby for CGI programming.
Next, let's spend some time editing Ruby CGI.
Write CGI scripts
The most script Ruby CGI code is as follows:
#! /Usr/bin/ruby puts "HTTP/1.0 200 OK" puts "Content-type: text/html \ n" puts "This is a test"
You can keep the code in the test. cgi file. The code can be executed as a CGI script after it is last sent to the server and granted sufficient permissions.
If your site address is http://www.example.com/, you can access the program with a http://www.example.com/test.cgi and the output is: "This is a test .".
After the browser accesses the URL, the Web server finds the test. cgi file in the site directory, parses the script code through the Ruby parser, and accesses the HTML document.
Use cgi. rb
Ruby can call the CGI library to write more complex CGI scripts.
The following code calls the CGI library to create a CGI script for the script.
#!/usr/bin/ruby require 'cgi' cgi = CGI.newputs cgi.headerputs "
In the following code, a CGI object is created and the header information is printed.
Form Processing
The CGI library can be used to obtain data submitted by forms (or parameters in the URL) in two ways, for example, URL:/cgi-bin/test. cgi? FirstName = Zara & LastName = Ali.
You can use CGI # [] to directly obtain the FirstName and LastName parameters:
#!/usr/bin/ruby require 'cgi'cgi = CGI.newcgi['FirstName'] # => ["Zara"]cgi['LastName'] # => ["Ali"]
Another method to obtain form data:
#!/usr/bin/ruby require 'cgi'cgi = CGI.newh = cgi.params # => {"FirstName"=>["Zara"],"LastName"=>["Ali"]}h['FirstName'] # => ["Zara"]h['LastName'] # => ["Ali"]
Run the following code to retrieve all key values:
#!/usr/bin/ruby require 'cgi'cgi = CGI.newcgi.keys # => ["FirstName", "LastName"]
If the form contains multiple fields with the same name, the values of the same field are saved in the array.
In the following example, specify the three identical fields "name" in the form with the values "Zara", "Huma", and "Nuha ":
#!/usr/bin/ruby require 'cgi'cgi = CGI.newcgi['name'] # => "Zara"cgi.params['name'] # => ["Zara", "Huma", "Nuha"]cgi.keys # => ["name"]cgi.params # => {"name"=>["Zara", "Huma", "Nuha"]}
Note: Ruby automatically determines the GET and POST methods, so you do not need to treat them differently.
The HML code is as follows:
Create Form and HTML
CGI contains a large number of methods to Create HTML. Each HTML tag has a corresponding method. Before using these methods, you must use CGI. new to create CGI objects.
To make tag nesting easier, these methods use content as a code block, and the code block returns a string as the TAG content. As follows:
#!/usr/bin/ruby require "cgi"cgi = CGI.new("html4")cgi.out{ cgi.html{ 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 } } }}
String escape
When processing URL parameters or HTML form data, You must escape the specified special characters, such as quotation marks (") and backslash (/).
The Ruby CGI object provides CGI. escape and CGI. unescape methods to handle escape of these special characters:
#!/usr/bin/ruby require 'cgi'puts CGI.escape(Zara Ali/A Sweet & Sour Girl")
The code execution result is as follows:
#!/usr/bin/ruby require 'cgi'puts CGI.escape(Zara Ali/A Sweet & Sour Girl")
Another group of instances:
#!/usr/bin/ruby require 'cgi'puts CGI.escapeHTML('
The code execution result is as follows: