This article mainly introduces the introduction of CGI programming with Ruby Guide, writing CGI script is also ruby for network programming, a basic function, the need for friends can refer to the
To write a CGI script:
The most basic ruby CGI script looks like this:
|
#!/usr/bin/ruby puts "http/1.0 OK" puts "content-type:text/htmlnn" puts " |
If you call this script test.cgi upload to a unix/linux web hosting service provider and have the appropriate permissions, you can use it as a CGI script.
For example, if you have a website hosted by a Linux web hosting provider, such as: Http://www.yiibai.com/test.cgi's home directory, and give it permission to execute, then access the http://www.yiibai.com/test.cgi Should return an HTML page display: This is a test.
Here when test.cgi requests from a Web browser, the Web server looks to execute it using the Ruby interpreter. The Ruby Script returns a basic HTTP header, and then returns a basic HTML document.
Use CGI.RB:
Ruby has a special called CGI library that makes more complex interactions than the previous CGI scripts.
Let's create a basic CGI using CGI script:
|
#!/usr/bin/ruby require ' cgi ' CGI = cgi.new puts Cgi.header puts ' |
Here, you create a CGI object and use it to print the header row.
Form processing:
There are two ways to access HTML query parameters using the CGI class. Suppose we give a/cgi-bin/test.cgi? Firstname=zara&lastname=ali.
You can access the parameters FirstName and LastName use cgi#[] as follows:
|
#!/usr/bin/ruby require ' cgi ' CGI = cgi.new cgi[' FirstName '] # => ["Zara"] cgi[' LastName '] # => ["Ali"] |
There is another way to access these form variables. This code gives the hash value of all the items and values:
|
#!/usr/bin/ruby require ' cgi ' CGI = cgi.new h = cgi.params # => {"FirstName" =>["Zara"], "LastName" =>["Ali"} h[' FirstName '] # => ["Zara"] h[' LastName '] # => ["Ali"] |
Here's the code to retrieve all the keys:
|
#!/usr/bin/ruby require ' cgi ' CGI = cgi.new Cgi.keys # => ["FirstName", "LastName"] |
If the form contains more than one field with the same name, the corresponding value is returned to the script as an array. [] Accessor returns these are only the first.
In this example, suppose there are three fields in the form named "name", we have entered three names "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 handle the Get and post methods. There are no separate ways to deal with these two different methods.
A related, but basic form, that can send the correct data, will have HTML code, just like this:
Create forms and HTML:
CGI contains a number of methods for creating HTML. You will find one of the methods for each label. To make these methods, you must create a CGI object by calling Cgi.new.
To make tags easier to nest, these methods take their content as a block of code. The code block returns a string, which will be used as the content of the label. For example:
|
#!/usr/bin/ruby require "cgi" CGI = cgi.new ("Html4") cgi.out{cgi.html{cgi.head{"n" +cgi.title{"This is a Test"} + CG i.body{"n" + cgi.form{"n" + cgi.hr + cgi.h1 {"A form:"} + "n" + cgi.textarea ("Get_text") + "n" + cgi.br + Cgi.submit}}} } |
Note: The method parameters of the CGI class can be accepted, which will set up the form submission used on the HTTP method (Get,post, etc.). By default, a post is used in this example.
This will produce the following results:
|
content-type:text/html content-length:302 <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 final//en" > <HTML> <HEAD> <title>this is a TEST</TITLE&G T </HEAD> <BODY> <form method= "POST" enctype= "application/x-www-form-urlencoded" > <HR> |
Reference string:
When dealing with URLs and HTML code, you must carefully reference some characters. For example, a slash character (/) has a special meaning in the URL, so it must be escaped if it is not part of the path name.
For example, the/query URL portion will be translated into a string%2f/and must be translated when used. Space and special characters with symbols. To deal with this problem, CGI provides: Cgi.escape and cgi.unescape in routine procedures.
|
#!/usr/bin/ruby require ' CGI ' puts Cgi.escape (Zara ali/a Sweet & Sour Girl ") |
This will produce the following results:
|
ZARA+ALI%2FA sweet+%26+sour+girl ") #!/usr/bin/ruby require ' CGI ' puts cgi.escapehtml (' |
This will produce the following results: