Getting started with Ruby for CGI programming

Source: Internet
Author: User
Tags html page require hosting web hosting

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:

?

1 2 3 4 5 #!/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:

?

1 2 3 4 5 6 7 #!/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:

?

1 2 3 4 5 6 #!/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:

?

1 2 3 4 5 6 7 #!/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:

?

1 2 3 4 5 #!/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":

?

1 2 3 4 5 6 7 8 #!/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:

?

1 2 3 4 5 6 7 8 9 10 11

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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 #!/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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 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.

?

1 2 3 4 #!/usr/bin/ruby require ' CGI ' puts Cgi.escape (Zara ali/a Sweet & Sour Girl ")

This will produce the following results:

?

1 2 3 4 5 6 ZARA+ALI%2FA sweet+%26+sour+girl ") #!/usr/bin/ruby require ' CGI ' puts cgi.escapehtml ('

This will produce the following results:

?

1
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.