Ruby CGI methods


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

##2345678910

CGI instantiation method

In the following example, we assign the object of CGI::new to the c variable. The method list is as follows:

Serial numberMethod description
1CGI::new([ level="query"])
Create a CGI object. query can be the following values:
  • query: No HTML generated output

  • html3: HTML3.2

  • html4: HTML4.0 Strict

  • html4Tr: HTML4.0 Transitional

  • html4Fr: HTML4.0 Frameset

CGI::escape(str)Use URL encoding to escape strings
CGI::unescape (str)Decode a string encoded by escape().
CGI::escapeHTML(str)Encode HTML special characters, including: & < >.
CGI::unescapeHTML(str)Decode HTML special characters, including: & < >.
CGI::escapeElement( str[, element...])Encode HTML in the specified HTML element Special characters.
CGI::unescapeElement(str, element[, element...])In the specified HTML element Decode HTML special characters.
CGI::parse(query)Parse the query string and return the key=》value containing the hash right.
CGI::pretty( string[, leader=" "])Return neat HTML format. If
leader is specified, it will be written to the beginning of each line. leader The default value is two spaces.
CGI::rfc1123_date(time)Format time according to RFC-1123 (for example, Tue, 2 Jun 2008 00:00:00 GMT).
22

HTML generation method

You can use the corresponding HTML tag name in the CGI instance to create HTML tags. The example is 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 use the following properties in CGI instances:

Serial numberMethod description
1c[ name]
Returns an array containing the value of the corresponding field named name.
2c.checkbox( name[, value[, check=false]])
c.checkbox( options)

Returns the HTML string used to define the checkbox field. The label's attributes can be passed as a parameter with a hash function.
3c.checkbox_group( name, value...)
c.checkbox_group( options)

> Returns the HTML string used to define the checkbox group. The label's attributes can be passed as a parameter with a hash function.
4c.file_field( name[, size=20[, max]])
c.file_field( options)

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

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

Return to define a HTML string for hidden fields. The label's attributes can be passed as a parameter with a hash function.
9c.image_button( url[, name[, alt]])
c.image_button( options)

Return the HTML string that defines an image button. The label's attributes can be passed as a parameter with a hash function.
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 the HTML string that defines a multimedia form (multipart). The label's attributes can be passed as a parameter with a hash function.
13c.out([ header]) { ...}
Generate HTML and output. Use the string generated by the block's output to create the body of the page.
14c.params
Returns a hash containing the form field name and value.
15c.params= hash
Set using field name and value.
16c.password_field( name[, value[, size=40[, max]]])
c.password_field( options )

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

Returns the HTML string that defines a pop-up menu. The label's attributes can be passed as a parameter with a hash function.
18c.radio_button( name[, value[, checked=false]])
c.radio_button( options)

Return the HTML string that defines a radio field. The label's attributes can be passed as a parameter with a hash function.
19c.radio_group( name, value...)
c.radio_group( options)

Return definition An HTML string for a radio button group. The label's attributes can be passed as a parameter with a hash function.
20c.reset( name[, value])
c.reset( options)

Return to define a HTML string for the reset button. The label's attributes can be passed as arguments to a hash function
21##c.text_field( name[, value[, size=40[, max ]]])c.text_field(options)
Returns the HTML string that defines a text field. The label's attributes can be passed as a parameter with a hash function.
c.textarea( name[, cols=70[, rows=10]]) { ...}c. textarea( options) { ...}
Returns the HTML string defining a textarea field. If block is specified, the string output by the code block will be used as the content of the textarea. The label's attributes can be passed as a parameter with a hash function.
##path_infoPathpath_translatedTranslated pathQuery_stringQuery stringrefererPreviously visited URLremote_addrClient host address (IP)remote_hostClient host nameremote_identClient nameremote_userAuthenticated Userrequest_methodRequest method (GET, POST, etc.)script_name Parameter nameserver_nameServer nameserver_portServer port server_protocolServer protocolserver_softwareServer softwareuser_agentUser agent
PropertiesReturn values
acceptAcceptable MIME types
accept_charsetAcceptable character set
accept_encodingAcceptable encoding
accept_languageAcceptable language
auth_typeAcceptable types
raw_cookieCookie data (raw string)
content_length Content length
content_typeContent type
From Client e-mail address
gateway_interfaceCGI version