Ruby Cgi Methods
The following is a list of methods for the CGI class:
| No. | Method Description |
| --- | --- |
| 1 | **CGI::new()** Creates a CGI object. The `query` parameter can have 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)** Escapes a string using URL encoding. |
| 3 | **CGI::unescape( str)** Decodes a string encoded by `escape()`. |
| 4 | **CGI::escapeHTML( str)** Encodes HTML special characters, including: &". |
| 5 | **CGI::unescapeHTML( str)** Decodes HTML special characters, including: &". |
| 6 | **CGI::escapeElement( str[, element...])** Encodes HTML special characters within specified HTML elements. |
| 7 | **CGI::unescapeElement( str, element[, element...])** Decodes HTML special characters within specified HTML elements. |
| 8 | **CGI::parse( query)** Parses a query string and returns a hash containing key=>value pairs. |
| 9 | **CGI::pretty( string[, leader=" "])** Returns neatly formatted HTML. If `_leader_` is specified, it is written at the beginning of each line. The default value for `_leader_` is two spaces. |
| 10 | **CGI::rfc1123_date( time)** Formats time according to RFC-1123 (e.g., Tue, 2 Jun 2008 00:00:00 GMT). |
* * *
## CGI Instance Methods
In the following examples, we assign the object from `CGI::new` to the variable `c`. The method list is as follows:
| No. | Method Description |
| --- | --- |
| 1 | **c** Returns an array containing the values corresponding to the field name `_name_`. |
| 2 | **c.checkbox( name[, value[, check=false]]) c.checkbox( options)** Returns an HTML string for defining a checkbox field. The tag's attributes can be passed as a hash function parameter. |
| 3 | **c.checkbox_group( name, value...) c.checkbox_group( options)** Returns an HTML string for defining a checkbox group. The tag's attributes can be passed as a hash function parameter. |
| 4 | **c.file_field( name[, size=20[, max]]) c.file_field( options)** Returns an HTML string for defining a file field. |
| 5 | **c.form([ method="post"[, url]]) { ...} c.form( options)** Returns an HTML string for defining a form. If a code block is specified, its output will be used as the form content. The tag's attributes can be passed as a hash function parameter. |
| 6 | **c.cookies** Returns a CGI::Cookie object containing the key-value pairs from the cookie. |
| 7 | **c.header()** Returns CGI header information. If the `header` parameter is a hash, its key-value pairs are used to create the header information. |
| 8 | **c.hidden( name[, value]) c.hidden( options)** Returns an HTML string for defining a hidden field. The tag's attributes can be passed as a hash function parameter. |
| 9 | **c.image_button( url[, name[, alt]]) c.image_button( options)** Returns an HTML string for defining an image button. The tag's attributes can be passed as a hash function parameter. |
| 10 | **c.keys** Returns an array containing the field names of the form. |
| 11 | **c.key?( name) c.has_key?( name) c.include?( name)** Returns true if the form contains the specified field name. |
| 12 | **c.multipart_form([ url[, encode]]) { ...} c.multipart_form( options) { ...}** Returns an HTML string for defining a multipart form. The tag's attributes can be passed as a hash function parameter. |
| 13 | **c.out() { ...}** Generates and outputs HTML. Uses the output from the block to create the page's body string. |
| 14 | **c.params** Returns a hash containing the form field names and values. |
| 15 | **c.params= hash** Sets the field names and values. |
| 16 | **c.password_field( name[, value[, size=40[, max]]]) c.password_field( options)** Returns an HTML string for defining a password field. The tag's attributes can be passed as a hash function parameter. |
| 17 | **c.popup_menu( name, value...) c.popup_menu( options) c.scrolling_list( name, value...) c.scrolling_list( options)** Returns an HTML string for defining a popup menu. The tag's attributes can be passed as a hash function parameter. |
| 18 | **c.radio_button( name[, value[, checked=false]]) c.radio_button( options)** Returns an HTML string for defining a radio button field. The tag's attributes can be passed as a hash function parameter. |
| 19 | **c.radio_group( name, value...) c.radio_group( options)** Returns an HTML string for defining a radio button group. The tag's attributes can be passed as a hash function parameter. |
| 20 | **c.reset( name[, value]) c.reset( options)** Returns an HTML string for defining a reset button. The tag's attributes can be passed as a hash function parameter. |
| 21 | **c.text_field( name[, value[, size=40[, max]]]) c.text_field( options)** Returns an HTML string for defining a text field. The tag's attributes can be passed as a hash function parameter. |
| 22 | **c.textarea( name[, cols=70[, rows=10]]) { ...} c.textarea( options) { ...}** Returns an HTML string for defining a textarea field. If a block is specified, the string output from the code block will be used as the textarea's content. The tag's attributes can be passed as a hash function parameter. |
* * *
## HTML Generation Methods
You can use the corresponding HTML tag names within a CGI instance to create HTML tags, as shown in the following example:
## Example
```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 within a CGI instance:
| Attribute | Return Value |
| --- | --- |
| accept | Acceptable MIME types |
| accept_charset | Acceptable character sets |
| accept_encoding | Acceptable encodings |
| accept_language | Acceptable languages |
| auth_type | Authentication type |
| raw_cookie | Cookie data (raw string) |
| content_length | Content length |
| content_type | Content type |
| From | Client email address |
| gateway_interface | CGI version |
| path_info | Path information |
| path_translated | Translated path |
| Query_string | Query string |
| referer | Referring URL |
| remote_addr | Client host address (IP) |
| remote_host | Client host name |
| remote_ident | Client identity |
| remote_user | Authenticated user |
| request_method | Request method (GET, POST, etc.) |
| script_name | Script name |
| server_name | Server name |
| server_port | Server port |
| server_protocol | Server protocol |
| server_software | Server software |
| user_agent | User agent |
YouTip