Getting started with CGI programming using Ruby and instructions on rubycgi Programming

Source: Internet
Author: User
Tags web hosting

Getting started with CGI programming using Ruby and instructions on rubycgi Programming

Compile the CGI script:

The most basic Ruby CGI script looks like this:

#! / usr / bin / ruby

puts "HTTP / 1.0 200 OK"
puts "Content-type: text / html \ n \ n"
puts "<html> <body> This is a test </ body> </ html>"

If you call this script test.cgi and upload it to a Unix / Linux-based web hosting service provider with 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, and give it execute permissions, then visit http: //www.yiibai .com / test.cgi should return an HTML page showing: This is a test.

Here when test.cgi is requested from a web browser, the web server looks to execute it using the Ruby interpreter. Ruby scripts return a basic HTTP header and then a basic HTML document.
Use cgi.rb:

Ruby has a special library called CGI that enables more complex interactions than previous CGI scripts.

Let's create a basic CGI script using CGI:

#! / usr / bin / ruby

require 'cgi'

cgi = CGI.new
puts cgi.header
puts "<html> <body> This is a test </ body> </ html>"

Here, a CGI object is created and used to print the header line.
Form processing:

There are two ways to use CGI classes to make HTML query parameters accessible. Suppose we give /cgi-bin/test.cgi?FirstName=Zara&LastName=Ali.

The parameters FirstName and LastName can be accessed using 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 a hash of all 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 is the code to retrieve all the keys:

#! / usr / bin / ruby

require 'cgi'
cgi = CGI.new
cgi.keys # => ["FirstName", "LastName"]

If the form contains multiple fields with the same name, the corresponding values will be returned to the script as an array. [] The accessor returns only the first of these.

In this example, suppose the form named "name" has three fields, and we enter the 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 GET and POST methods. There is no separate approach to these two different approaches.

A related but basic form that can send the correct data, there will be HTML code, like this:

<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>

Create the form and HTML:

CGI contains a number of methods for creating HTML. You will find one of the methods for each label. To enable these methods, a CGI object must be created by calling CGI.new.

To make tags easier to nest, these methods take their content as code blocks. The code block returns a string, which will be used as the label's content. E.g:

#! / 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
     }
   }
  }
}

Note: CGI class methods can accept method parameters, which will set the form submission used on HTTP methods (GET, POST, etc.). By default, 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>
</ HEAD>
<BODY>
<FORM METHOD = "post" ENCTYPE = "application / x-www-form-urlencoded">
<HR>
<H1> A Form: </ H1>
<TEXTAREA COLS = "70" NAME = "get_text" ROWS = "10"> </ TEXTAREA>
<BR>
<INPUT TYPE = "submit">
</ FORM>
</ BODY>
</ HTML>

Quote string:

When dealing with URL and HTML code, you must be careful to quote some characters. For example, a slash character (/) has special meaning in a URL, so it must be escaped if it is not a partial path name.

For example, the / query URL part will be translated into the string% 2F / and must be translated when using it. Space and special characters with symbols. To deal with this, CGI provides: in the routines CGI.escape and CGI.unescape.

#! / 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 ('<h1> Zara Ali / A Sweet & Sour Girl </ h1>')

This will produce the following results:

<h1> Zara Ali / A Sweet & Sour Girl </ h1> '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.