CGI

GET

require 'cgi'
cgi = CGI.new
paramValue = cgi['param_name']

# Store all of the parameters in a hash
values = cgi.params
  • If the parameter was not sent, the value retrieved is an empty string.

POST

require 'cgi'
cgi = CGI.new
paramValue = cgi['param_name']

# Store all of the parameters in a hash
values = cgi.params
  • If the parameter was not sent, the value retrieved is an empty string.
  • If form data is POST'ed, can't retrieve any separate URL parameters in the above manner

Environment variables

host      = ENV['HTTP_HOST']
url = ENV['REQUEST_URI']

base_url = ENV['SCRIPT_NAME]
url_query = ENV['QUERY_STRING']

HTML forms


Text field

  • If the field is empty, the value retrieved is an empty string.

Radio button

  • If no item was selected, the value retrieved is an empty string.

Check box

  • If the field is unchecked, the value retrieved is an empty string.

Single-selection list

  • If no item was selected, the value retrieved is an empty string.

Multiple-selection list

  • The value retrieved can be converted to an array (to_a).
  • If no items were selected, the converted value is an empty array.