Defindit Docs and Howto Home

This page last modified: Sep 16 2010
keywords:rails,ruby,environment,webrick,server,variables,http,request,attributes,cgi,http,get,post,remote_addr,remote_host,script_name,server_name,server_port
description:How to access CGI environment variables from Ruby applications running under Rails.
title:Rails CGI HTTP variables

Table of contents
-----------------
Introduction
Working code snippet
List of public methods



Introduction
------------

If you are running a Rails application (especially with Webrick), the
CGI environment and CGI object seems not to work. (The CGI object has
all nill properties.) This is probably because Rails has co-opted all
the properties. Instead you muct use the Rails request object which
has the necessary methods (and perhaps attributes aka instance
variables).

While Class CGI documentation implies that environment variables such
as SERVER_PORT are available as attributes, I couldn't find any
examples, and when I created an example that ran without errors, the
value was alway nil. I think the use of Class CGI assumes you are
*not* running Rails.

Using the request object from within a Rails controller works as
expected (given that I never did find documentation for the request
object).

I haven't tried this with Apache httpd, only with Webrick.



Working code snippet
--------------------

The code below is working example code. I added this code to a working
Rails controller:

	    
    eport = request.server_port
    @message.concat "eport: #{eport}\n" 

    request.public_methods.sort.each { |var|
       @message.concat "#{var}\n"
     }

The .erb simply displays @message. Add this HTML snippet somewhere in
the <body> tag of your controller's .erb file:

  <pre><%= @message %></pre>




List of public methods
----------------------

Using the code snippet above, there are the methods returned for the
version of Rails I'm running. There are methods to access what would
normally be variables or properties. For example this Perl code from a
CGI script:

ENV{SERVER_PORT}

in Rails would be:

request.server_port




==
===
=~
GET
POST
[]
[]=
__id__
__send__
`
accept
accept_charset
accept_encoding
accept_language
accepts
acts_like?
as_json
auth_type
b64encode
blank?
body
body_stream
breakpoint
cache_control
cache_format
class
class_eval
clone
content_charset
content_length
content_type
cookies
copy_instance_variables_from
daemonize
debugger
decode64
decode_b
delete?
display
domain
dup
duplicable?
enable_warnings
encode64
enum_for
env
eql?
equal?
etag_matches?
extend
extend_with_included_modules_from
extended_by
form_data?
format
format=
freeze
fresh?
from
frozen?
fullpath
gateway_interface
get?
hash
head?
headers
host
host_with_port
html_safe?
id
if_modified_since
if_none_match
inspect
instance_eval
instance_exec
instance_of?
instance_values
instance_variable_defined?
instance_variable_get
instance_variable_names
instance_variable_set
instance_variables
ip
is_a?
key?
kind_of?
load_with_new_constant_marking
logger
media_type
media_type_params
metaclass
metaclass_with_deprecation
metaclass_without_deprecation
method
methods
negotiate
nil?
not_modified?
object_id
parameters
params
parseable_data?
path
path_info
path_info=
path_parameters
path_parameters=
path_translated
port
port_string
post?
pragma
presence
present?
private_methods
protected_methods
protocol
public_methods
put?
query_parameters
query_string
raw_host_with_port
raw_post
referer
referrer
remote_addr
remote_host
remote_ident
remote_ip
remote_user
remove_subclasses_of
request_method
request_parameters
request_uri
require
require_association
require_dependency
require_library_or_gem
require_or_load
reset_session
respond_to?
returning
scheme
script_name
script_name=
send
server_name
server_port
server_protocol
server_software
session
session=
session_options
session_options=
silence_stderr
silence_stream
silence_warnings
singleton_class
singleton_methods
ssl?
standard_port
subclasses_of
subdomains
suppress
symbolized_path_parameters
taguri
taguri=
taint
tainted?
tap
template_format
to_a
to_enum
to_json
to_param
to_query
to_s
to_yaml
to_yaml_properties
to_yaml_style
try
type
unloadable
untaint
url
user_agent
values_at
with_options
xhr?
xml_http_request?