English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby CGI Methods

Ruby CGI Methods

The following is a list of methods for the CGI class:

CGI instantiation methodIn the following examples, we assign the object of CGI::new to the variable c, and the method list is as follows:
1CGI::new([ level="query"])
Create CGI objects. query can be one of the following values:
  • query: No HTML output generated

  • html3: HTML3.2

  • html4:  HTML4.0 Strict

  • html4Tr: HTML4.0 Transitional

  • html4Fr:  HTML4.0 Frameset

2CGI::escape( str)
Use URL encoding to escape strings
3CGI::unescape( str)
Decode strings encoded with escape().
4CGI::escapeHTML( str)
Encode HTML special characters, including: & < >.
5CGI::unescapeHTML( str)
Decode HTML special characters, including: & < >.
6CGI::escapeElement( str[, element...])
Encode HTML special characters within specified HTML elements.
7CGI::unescapeElement( str, element[, element...])
在指定的 HTML 元素中解码 HTML 特殊字符。
8CGI::unescapeElement(str, element[, element...])
Decode HTML special characters in the specified HTML element.
9CGI::parse(query)
Parse the query string and return a hash containing key=>value pairs. Returns neatly formatted HTML. If specified CGI::pretty(string[, leader=" "]) Returns neatly formatted HTML. If specified which will be written at the beginning of each line.
10leader1123The default value is two spaces.
CGI::rfc-1123 _date(time) 2 According to RFC 2To format time (for example, Tue,8 Jun

00

00:00:00 GMT).

CGI instantiation methodIn the following examples, we assign the object of CGI::new to the variable c, and the method list is as follows:
1Serial number
Method description c[name] Returns an array containing the corresponding field name
2name
The value.

Returns an HTML string for defining a checkbox field. The tag attributes can be passed as a hash function parameter.
3c.checkbox_group(name, value...)
c.checkbox_group(options)

>Returns an HTML string for defining a checkbox group. The tag attributes can be passed as a hash function parameter.
4c.file_field(name[, size=20[, max]])
c.file_field(options)

Returns the HTML string defined by the file field.
5c.form([method="post"[, url]]) { ...}
c.form(options)

Returns the HTML string defined by the form. If a code block is specified, it will be output as the form content. The tag attributes can be passed as a hash function parameter.
6c.cookies
Returns the CGI::Cookie object, which contains key-value pairs in the cookie.
7c.header([header])
Returns information of the CGI header. If the header parameter is a hash, the key - Value pairs used to create header information.
8c.hidden(name[, value])
c.hidden(options)

Returns an HTML string for defining a hidden field. The tag attributes can be passed as a hash function parameter.
9c.image_button( url[, name[, alt]])
c.image_button( options)

Returns an HTML string for defining an image button. The tag attributes can be passed as a hash function parameter.
10c.keys
Returns an array containing the field names of the form.
11c.key?( name)
c.has_key?( name)
c.include?( name)

Returns true if the form contains the specified field name.
12c.multipart_form([ url[, encode]]) { ...}
c.multipart_form( options) { ...}

Returns an HTML string for defining a multimedia form (multipart). The tag attributes can be passed as a hash function parameter.
13c.out([ header]) { ...}
Generates HTML and outputs. Use the string generated by the block output to create the main content of the page.
14c.params
Returns a hash containing the form field names and values.
15c.params= hash
Sets the field name and value to be used.
16c.password_field( name[, value[, size=40[, max]]
c.password_field( options)

Returns an HTML string for defining a password field. The tag attributes can be passed as a hash function parameter.
17c.popup_menu( name, value...)
c.popup_menu( options)
c.scrolling_list( name, value...)
c.scrolling_list( options)

Returns an HTML string for defining a dropdown menu. The tag attributes can be passed as a hash function parameter.
18c.radio_button( name[, value[, checked=false]])
c.radio_button( options)

Returns an HTML string for defining a radio field. The tag attributes can be passed as a hash function parameter.
19c.radio_group( name, value...)
c.radio_group( options)

Returns an HTML string for defining a radio button group. The tag attributes can be passed as a hash function parameter.
20c.reset( name[, value])}}
c.reset( options)

Returns a string of HTML to define a reset button. The tag attributes can be passed as a hash function as a parameter.
21c.text_field( name[, value[, size=40[, max]]
c.text_field( options)

Returns a string of HTML to define a text field. The tag attributes can be passed as a hash function as a parameter.
22c.textarea( name[, cols=70[, rows=10]] { ...}
c.textarea( options) { ...}

Returns a string of HTML to define a textarea field. If a block is specified, the string output by the code block will be used as the content of the textarea. The tag attributes can be passed as a hash function as a parameter.

HTML Generation Method

You can use the corresponding HTML tag names in the CGI example to create HTML tags, for example:

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

You can use the following properties in the CGI example:

PropertiesReturn value
acceptAcceptable MIME types
accept_charsetAcceptable character sets
accept_encodingAcceptable encodings
accept_languageAcceptable languages
auth_typeAcceptable types
raw_cookieCookie data (original string)
content_lengthContent length
content_typeContent type
FromClient e-mail address
gateway_interfaceCGI Version
path_infoPath
path_translatedTranslated Path
Query_stringQuery String
refererPrevious Visited URL
remote_addrClient Host Address (IP)
remote_hostClient Hostname
remote_identClient Name
remote_userAuthenticated User
request_methodRequest Method (GET, POST, etc.)
script_nameParameter Name
server_nameServer Name
server_portServer Port
server_protocolServer Protocol
server_softwareServer Software
user_agentUser Agent (User agent)