Latest web development tutorials

Ruby CGI method

The following is a list of methods CGI class:

No. Method Description
1 CGI :: new ([level = " query"])
Create CGI object. query can be the following values:
  • query: no HTML output generated

  • html3: HTML3.2

  • html4: HTML4.0 Strict

  • html4Tr: HTML4.0 Transitional

  • html4Fr: HTML4.0 Frameset

2 CGI :: escape (str)
URL-encoded string to escape
3 CGI :: unescape (str)
Through the escape () to decode the encoded string.
4 CGI :: escapeHTML (str)
Coding HTML special characters, including: & <>.
5 CGI :: unescapeHTML (str)
Decode special HTML characters, including: & <>.
6 CGI :: escapeElement (str [, element ...])
Coding HTML special characters in the specified HTML element.
7 CGI :: unescapeElement (str, element [ , element ...])
Decode special HTML characters in the specified HTML element.
8 CGI :: parse (query)
Parse the query string and returns a hash containing key = "value pairs.
9 CGI :: pretty (string [, leader = ""])
Back neat HTML format. If you specify aleader,it is written to the beginning of each line.leaderdefault is two spaces.
10 CGI :: rfc1123_date (time)
According to RFC-1123 format time (for example, Tue, 2 Jun 2008 00:00:00 GMT).

CGI instance method

The following examples will CGI :: new object assigned to the variable c, methods listed below:

No. Method Description
1 c [name]
It returns an array that contains the value of the corresponding field namedname.
2 c.checkbox (name [, value [, check = false]])
c.checkbox (options)
Returns an HTML string that defines the checkbox field. Property of the label may be a hash function passed as a parameter.
3 c.checkbox_group (name, value ...)
c.checkbox_group (options)
> Returns HTML string used to define the checkbox group. Property of the label may be a hash function passed as a parameter.
4 c.file_field (name [, size = 20 [, max]])
c.file_field (options)
Returns the definition file field HTML string.
5 c.form ([method = "post" [, url]]) {...}
c.form (options)
Back defined form form HTML string. If a block is specified, the output as the contents of the form. Property of the label may be a hash function passed as a parameter.
6 c.cookies
Back CGI :: Cookie object containing the cookie in pairs.
7 c.header ([header])
Back CGI header information. If the header argument is a hash value, which is the key - value pairs are used to create the header information.
8 c.hidden (name [, value])
c.hidden (options)
Returns an HTML string to define a hidden field. Property of the label may be a hash function passed as a parameter.
9 c.image_button (url [, name [, alt]])
c.image_button (options)
Back to define a string of HTML image button. Property of the label may be a hash function passed as a parameter.
10 c.keys
It returns an array that contains a form field names.
11 c.key? (name)
c.has_key? (name)
c.include? (name)
If the form contains the specified field name returns true.
12 c.multipart_form ([url [, encode] ]) {...}
c.multipart_form (options) {...}
Back define a multimedia forms (multipart) HTML string. Property of the label may be a hash function passed as a parameter.
13 c.out ([header]) {... }
And generates HTML output. Output block used by the body to create a page generated string.
14 c.params
Returns hash contains form field names and values.
15 c.params = hash
Setting using field names and values.
16 c.password_field (name [, value [, size = 40 [, max]]])
c.password_field (options)
Back define a password field HTML string. Property of the label may be a hash function passed as a parameter.
17 c.popup_menu (name, value ...)
c.popup_menu (options)
c.scrolling_list (name, value ...)
c.scrolling_list (options)
Returns a pop-up menu to define the HTML string. Property of the label may be a hash function passed as a parameter.
18 c.radio_button (name [, value [, checked = false]])
c.radio_button (options)
Returns a string of HTML defined radio field. Property of the label may be a hash function passed as a parameter.
19 c.radio_group (name, value ...)
c.radio_group (options)
Returns a string defined HTML radio button group. Property of the label may be a hash function passed as a parameter.
20 c.reset (name [, value])
c.reset (options)
Define a reset button to return HTML string. Property of the label may be a hash function as an argument
twenty one c.text_field (name [, value [, size = 40 [, max]]])
c.text_field (options)
Returns an HTML string fields defined text. Property of the label may be a hash function passed as a parameter.
twenty two c.textarea (name [, cols = 70 [, rows = 10]]) {...}
c.textarea (options) {...}
Returns a string defined HTML textarea field. If you specify a block, a block of code strings output as the content of the textarea. Property of the label may be a hash function passed as a parameter.

HTML Generation

You can then use the appropriate CGI example HTML tag names to create HTML tags, examples are 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
         }
      }
   }
}

CGI object properties

You can then use the CGI instance the following properties:

Attributes return value
accept Acceptable MIME type
accept_charset Acceptable character sets
accept_encoding Acceptable coding
accept_language Acceptable language
auth_type Acceptable Types
raw_cookie Cookie data (original string)
content_length Content length (Content length)
content_type Content type (Content type)
From Client e-mail address
gateway_interface CGI version
path_info path
path_translated After the conversion path
Query_string Query String
referer Before accessing the URL
remote_addr Client host address (IP)
remote_host Client hostname
remote_ident Client name
remote_user Authenticated users
request_method Request method (GET, POST, etc.)
script_name Parameter name
server_name server name
server_port Server port
server_protocol Server protocol
server_software Server Software
user_agent User Agent (User agent)