Creates a"> Creates a" />
Easy Tutorial
❮ Ruby Tutorial Ruby Cgi ❯

Ruby CGI Methods

Below is a list of methods for the CGI class:

No. Method Description
1 CGI::new([ level="query"]) <br>Creates a CGI object. query can be one of the following values: query: No HTML output generated<br>html3: HTML3.2<br>html4: HTML4.0 Strict<br>html4Tr: HTML4.0 Transitional<br>html4Fr: HTML4.0 Frameset
2 CGI::escape( str) <br>Escapes a string using URL encoding
3 CGI::unescape( str) <br>Decodes a string encoded with escape()
4 CGI::escapeHTML( str) <br>Encodes HTML special characters, including: & < >
5 CGI::unescapeHTML( str) <br>Decodes HTML special characters, including: & < >
6 CGI::escapeElement( str[, element...]) <br>Encodes HTML special characters within specified HTML elements
7 CGI::unescapeElement( str, element[, element...]) <br>Decodes HTML special characters within specified HTML elements
8 CGI::parse( query) <br>Parses a query string and returns a hash of key=>value pairs
9 CGI::pretty( string[, leader=" "]) <br>Returns neatly formatted HTML. If leader is specified, it will be written at the beginning of each line. Default leader is two spaces.
10 CGI::rfc1123_date( time) <br>Formats the time according to RFC-1123 (e.g., Tue, 2 Jun 2008 00:00:00 GMT)

CGI Instantiation Methods

In the following examples, we assign the object created by CGI::new to the variable c. The methods are listed below:

No. Method Description
1 c[ name] <br>Returns an array containing the values for the field named name
2 c.checkbox( name[, value[, check=false]])<br>c.checkbox( options) <br>Returns an HTML string to define a checkbox field. Tag attributes can be passed as a hash.
3 c.checkbox_group( name, value...)<br>c.checkbox_group( options) <br>Returns an HTML string to define a checkbox group. Tag attributes can be passed as a hash.
4 c.file_field( name[, size=20[, max]])<br>c.file_field( options) <br>Returns an HTML string to define a file field.
5 c.form([ method="post"[, url]]) { ...}<br>c.form( options) <br>Returns an HTML string to define a form. If a block is specified, it will be output as the form content. Tag attributes can be passed as a hash.
6 c.cookies <br>Returns a CGI::Cookie object containing key-value pairs from cookies
7 c.header([ header]) <br>Returns CGI header information. If header is a hash, its key-value pairs are used to create the header.
8 c.hidden( name[, value])<br>c.hidden( options) <br>Returns an HTML string to define a hidden field. Tag attributes can be passed as a hash.
9 c.image_button( url[, name[, alt]])<br>c.image_button( options) <br>Returns an HTML string to define an image button. Tag attributes can be passed as a hash.
10 c.keys <br>Returns an array containing the field names of the form
11 c.key?( name)<br>c.has_key?( name)<br>c.include?( name) <br>Returns true if the form includes the specified field name
12 c.multipart_form([ url[, encode]]) { ...}<br>c.multipart_form( options) { ...} <br>Returns an HTML string to define a multipart form. Tag attributes can be passed as a hash.
13 c.out([ header]) { ...} <br>Generates and outputs HTML. Uses the output from the block to create the page body.
14 c.params <br>Returns a hash containing field names and values from the form
15 c.params= hash <br>Sets field names and values
16 c.password_field( name[, value[, size=40[, max]]])<br>c.password_field( options) <br>Returns an HTML string to define a password field. Tag attributes can be passed as a hash.
17 c.popup_menu( name, value...)<br>c.popup_menu( options)<br>c.scrolling_list( name, value...)<br>c.scrolling_list( options) <br>Returns an HTML string to define a popup menu. Tag attributes can be passed as a hash.
18 c.radio_button( name[, value[, checked=false]])<br>c.radio_button( options) <br>Returns an HTML string to define a radio button. Tag attributes can be passed as a hash.
19 c.radio_group( name, value...)<br>c.radio_group( options) <br>Returns an HTML string to define a radio button group. Tag attributes can be passed as a hash.
20 c.reset( name[, value])<br>c.reset( options) <br>Returns an HTML string to define a reset button. Tag attributes can be passed as a hash.
21 c.text_field( name[, value[, size=40[, max]]])<br>c.text_field( options) <br>Returns an HTML string to define a text field. Tag attributes can be passed as a hash.
22 c.textarea( name[, cols=70[, rows=10]]) { ...}<br>c.textarea( options) { ...} <br>Returns an HTML string to define a textarea field. If a block is specified, the block output will be the content of the textarea. Tag attributes can be passed as a hash.

HTML Generation Methods

You can use the corresponding HTML tag names in a CGI instance to create HTML tags. Here is an example:

Example

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

You can use the following attributes in a CGI instance:

Attribute Return Value
accept Accepted MIME types
accept_charset Accepted character sets
accept_encoding Accepted encodings
accept_language Accepted languages
auth_type Accepted types
raw_cookie Cookie data (raw string)
content_length Content length
content_type Content type
From Client e-mail address
gateway_interface CGI version
path_info Path
path_translated Translated path
Query_string Query string
referer Previous visited URL
remote_addr Client host address (IP)
remote_host Client host name
remote_ident Client name
remote_user Authenticated user
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
❮ Ruby Tutorial Ruby Cgi ❯