Sunday, April 03, 2011

Mispelled HTTP_REFERER in Rails 3.0

The correct spelling is HTTP_REFERRER but Rails source has it with wrong spelling. So in order to get the HTTP_REFERRER do the following in your controller:

request.env["HTTP_REFERER"]

request.domain has been deprecated. The domain method has been moved to ActionDispatch::Http::URL. Ideally we want to use the method defined in this module.Do to metaprogramming it is difficult to figure out the actual keys that are used in ActionDispatch::Request. If you run this snippet:

request.env.keys.each do |x|
logger.info x
end

You will get all the keys:

action_dispatch.request.formats
action_dispatch.request.parameters
rack.session
HTTP_ACCEPT
HTTP_HOST
SERVER_NAME
rack.request.cookie_hash
action_dispatch.remote_ip
rack.url_scheme
HTTP_KEEP_ALIVE
HTTP_USER_AGENT
REQUEST_PATH
action_dispatch.request.query_parameters
action_dispatch.request.unsigned_session_cookie
SERVER_PROTOCOL
HTTP_ACCEPT_LANGUAGE
rack.errors
action_dispatch.secret_token
async.callback
REMOTE_ADDR
PATH_INFO
rack.run_once
rack.version
SERVER_SOFTWARE
action_dispatch.request.path_parameters
rack.request.cookie_string
SCRIPT_NAME
HTTP_REFERER
action_dispatch.parameter_filter
HTTP_COOKIE
HTTP_VERSION
rack.multithread
action_dispatch.request.request_parameters
action_dispatch.cookies
REQUEST_URI
rack.multiprocess
rack.request.query_hash
SERVER_PORT
HTTP_ACCEPT_CHARSET
action_controller.instance
rack.session.options
async.close
REQUEST_METHOD
warden
rack.request.query_string
action_dispatch.request.content_type
GATEWAY_INTERFACE
HTTP_CONNECTION
HTTP_ACCEPT_ENCODING
QUERY_STRING
rack.input

Now you can access any of these environment values from your controller similar to the referrer.