Ruby is a universal language, not just a language for web development, but Ruby's development in Web applications and Web tools is the most common.
Using Ruby you can not only write your own SMTP server, FTP program, or ruby Web server, but you can also use Ruby for CGI programming.
Next, let's take some time to school Ruby's CGI editor.
Writing CGI Scripts
The most scripted Ruby CGI code is as follows:
#!/usr/bin/ruby puts "http/1.0 OK" puts "content-type:text/html\n\n" puts "This is a test"
You can keep the code in the test.cgi file, last to the server and given sufficient permissions to execute as a CGI script.
If you are standing at the address of http://www.example.com/, you can use HTTP://WWW.EXAMPLE.COM/TEST.CGI to access the program, the output is: "The is a test."
After the browser accesses the URL, the Web server finds the test.cgi file in the site directory and then parses the script code and accesses the HTML document through the Ruby parser.
Using CGI.RB
Ruby can invoke the CGI library to write more complex CGI scripts.
The following code calls the CGI library to create a scripted CGI script.
#!/usr/bin/ruby require ' cgi ' CGI = cgi.newputs cgi.headerputs "This is a test"
In the following code, a CGI object is created and the header information is printed.
Form processing
There are two ways to get data for a form submission (or a parameter in a URL) using the CGI library, such as url:/cgi-bin/test.cgi? Firstname=zara&lastname=ali.
You can use cgi#[] to get parameters FirstName and LastName directly:
#!/usr/bin/ruby require ' cgi ' CGI = cgi.newcgi[' FirstName '] # = ["Zara"]cgi[' LastName '] # = = ["Ali"]
Another way to get the form data:
#!/usr/bin/ruby require ' cgi ' cgi = CGI.NEWH = cgi.params # + = {"FirstName" =>["Zara"], "LastName" =>["Ali"]}h[' FirstName ' # = ["Zara"]h[' LastName '] # = ["Ali"]
The following code is used to retrieve all the key values:
#!/usr/bin/ruby require ' cgi ' CGI = CGI.newcgi.keys # = ["FirstName", "LastName"]
If the form contains more than one field of the same name, the value of the same field is saved in the array.
In the following example, the three identical fields "name" in the form are specified, with the values "Zara", "Huma", and "Nuha", respectively:
#!/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 there is no need to treat the two methods differently.
The following is the relevant HML code:
Creating form forms and HTML
CGI contains a number of ways to create HTML, and each HTML tag has a corresponding method. Before using these methods, you must create a CGI object over Cgi.new.
To make the nesting of tags more simple, these methods use the content as a block of code, and the code block returns a string as the contents of the label. As shown below:
#!/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 you are working with parameters in a URL or HTML form data, you need to escape the specified special characters, such as quotation marks ("), and backslashes (/).
Ruby CGI objects provide the Cgi.escape and Cgi.unescape methods to handle the escape of these special characters:
#!/usr/bin/ruby require ' CGI ' puts Cgi.escape (Zara ali/a Sweet & Sour Girl ")
The result of the above code execution is as follows:
#!/usr/bin/ruby require ' CGI ' puts Cgi.escape (Zara ali/a Sweet & Sour Girl ")
Another set of instances:
#!/usr/bin/ruby require ' CGI ' puts cgi.escapehtml ('Zara ali/a Sweet & Sour Girl
')
The result of the above code execution is as follows:
Zara ali/a Sweet & Sour Girl
'