Ruby CGI Methods
The following is a list of methods of the CGI class:
| No. | Method Description |
|---|---|
| 1 | CGI::new([ level="query"]) Create a CGI object. query can be the following values:
|
| 2 | CGI::escape( str) Use URL encoding to escape the string. |
| 3 | CGI::unescape( str) Decode a string encoded by escape(). |
| 4 | CGI::escapeHTML( str) Encode HTML special characters, including: & < >. |
| 5 | CGI::unescapeHTML( str) Decode HTML special characters, including: & < >. |
| 6 | CGI::escapeElement( str[, element...]) Encode HTML special characters within the specified HTML element. |
| 7 | CGI::unescapeElement( str, element[, element...]) Decode HTML special characters within the specified HTML element. |
| 8 | CGI::parse( query) Parse the query string and return a hash containing key=>value pairs. |
| 9 | CGI::pretty( string[, leader=" "]) Returns neatly formatted HTML. If specifiedleaderit will be written to the beginning of each line.leaderThe default value is two spaces. |
| 10 | CGI::rfc1123_date( time) Format time according to RFC-1123 (e.g., Tue, 2 Jun 2008 00:00:00 GMT). |
CGI Instance Methods
In the following example, we assign the CGI::new object to variable c. The method list is as follows:
| No. | Method Description |
|---|---|
| 1 | c[ name] Returns an array containing the corresponding field namenamevalue. |
| 2 | c.checkbox( name[, value[, check=false]]) c.checkbox( options) Returns an HTML string for defining a checkbox field. Tag attributes can be passed as a hash argument. |
| 3 | c.checkbox_group( name, value...) c.checkbox_group( options) > Returns an HTML string for defining a checkbox group. Tag attributes can be passed as a hash argument. |
| 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, it will be output as the form content. Tag attributes can be passed as a hash argument. |
| 6 | c.cookies Returns a CGI::Cookie object containing the key-value pairs from the cookie. |
| 7 | c.header([ header]) Returns the 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. Tag attributes can be passed as a hash argument. |
| 9 | c.image_button( url[, name[, alt]]) c.image_button( options) Returns an HTML string for defining an image button. Tag attributes can be passed as a hash argument. |
| 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. Tag attributes can be passed as a hash argument. |
| 13 | c.out([ header]) { ...} Generate HTML and output it. Use the output of the block to create the string generated for the page body. |
| 14 | c.params Returns a hash containing form field names and values. |
| 15 | c.params= hash Set the field name and value to use. |
| 16 | c.password_field( name[, value[, size=40[, max]]]) c.password_field( options) Returns an HTML string for defining a password field. Tag attributes can be passed as a hash argument. |
| 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 pop-up menu. Tag attributes can be passed as a hash argument. |
| 18 | c.radio_button( name[, value[, checked=false]]) c.radio_button( options) Returns an HTML string for defining a radio field. Tag attributes can be passed as a hash argument. |
| 19 | c.radio_group( name, value...) c.radio_group( options) Returns an HTML string for defining a radio button group. Tag attributes can be passed as a hash argument. |
| 20 | c.reset( name[, value]) c.reset( options) Returns an HTML string for defining a reset button. Tag attributes can be passed as a hash argument. |
| 21 | c.text_field( name[, value[, size=40[, max]]]) c.text_field( options) Returns an HTML string for defining a text field. Tag attributes can be passed as a hash argument. |
| 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 by the block will be used as the content of the textarea. Tag attributes can be passed as a hash argument. |
HTML Generation Methods
You can use the corresponding HTML tag names in a CGI instance to create HTML tags, as shown in the following 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 | Acceptable MIME types |
| accept_charset | Acceptable character sets |
| accept_encoding | Acceptable encodings |
| accept_language | Acceptable languages |
| auth_type | Acceptable 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 | Previously 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 names |
| server_name | Server name |
| server_port | Server port |
| server_protocol | Server protocol |
| server_software | Server software |
| user_agent | User agent |