Ruby CGI Methods
The following is a list of CGI class methods:
| No. | Method Description |
|---|---|
| 1 | CGI::new([ level="query"]) |
创建 CGI 对象。query可以是以下值:
query: No HTML generation output
html3: HTML3.2
html4: HTML4.0 Strict
html4Tr: HTML4.0 Transitional
html4Fr: HTML4.0 Frameset
CGI instance methods
In the following examples, we assign the CGI::new object to the variable c; the method list is as follows:
| No. | Method Description |
|---|---|
| 1 | c[ name] |
| Returns an array containing the values corresponding to the field name name value. | |
| 2 | **c.checkbox( name[, value[, check=false]]) |
c.checkbox( options)**
Returns an HTML string defining a checkbox field. The tag's attributes can be passed as a hash argument. | | 3 | c.checkbox_group( name, value...)
c.checkbox_group( options)
>Returns an HTML string defining a checkbox group. The tag's attributes can be passed as a hash argument. | | 4 | c.file_field( name[, size=20[, max]])
c.file_field( options)
Returns an HTML string defining a file field. | | 5 | c.form([ method="post"[, url]]) { ...}
c.form( options)
Returns an HTML string defining a form. If a block is specified, it is output as the form's content. The tag's attributes can be passed as a hash argument. | | 6 | c.cookies
Returns a CGI::Cookie object containing the key-value pairs of the cookie. | | 7 | c.header([ header])
Returns the CGI header information. If the header argument is a hash, its key-value pairs are used to create the header. | | 8 | c.hidden( name[, value])
c.hidden( options)
Returns an HTML string defining a hidden field. The tag's attributes can be passed as a hash argument. | | 9 | c.image_button( url[, name[, alt]])
c.image_button( options)
Returns an HTML string defining an image button. The tag's attributes can be passed as a hash argument. | | 10 | c.keys
Returns an array containing the form's field names. | | 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 defining a multipart form. The tag's attributes can be passed as a hash argument. | | 13 | c.out([ header]) { ...}
Generates and outputs HTML. Uses the string generated by the block's output to create the page body. | | 14 | c.params
Returns a hash containing form field names and values. | | 15 | c.params= hash
Sets the field names and values to use. | | 16 | c.password_field( name[, value[, size=40[, max]]])
c.password_field( options)
Returns an HTML string defining a password field. The tag's 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 defining a popup menu. The tag's attributes can be passed as a hash argument. | | 18 | c.radio_button( name[, value[, checked=false]])
c.radio_button( options)
Returns an HTML string defining a radio field. The tag's attributes can be passed as a hash argument. | | 19 | c.radio_group( name, value...)
c.radio_group( options)
Returns an HTML string defining a radio button group. The tag's attributes can be passed as a hash argument. | | 20 | c.reset( name[, value])
c.reset( options)
Returns an HTML string defining a reset button. The tag's 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 defining a text field. The tag's attributes can be passed as a hash argument. | | 22 | c.textarea( name[, cols=70[, rows=10]]) { ...}
c.textarea( options) { ...}
Returns an HTML string defining a textarea field. If a block is specified, the string output by the block becomes the textarea's content. The tag's 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 below:
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 a CGI instance:
| property | 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 name |
| server_name | Server name |
| server_port | Server port |
| server_protocol | Server protocol |
| server_software | Server software |
| user_agent | User agent |