views:

1717

answers:

4

We are building a plugin for Rails to be used within iframe Facebook applications, and at one point we need to check if Rail's session id cookie as been set or not.

By default, this cookie is named _myprojectname_session, what we need to find out is the actual name of the cookie itself. So if it's not set, we can do some redirects to make sure the cookies are set.

How do we access the damn name of the cookie from anywhere? Or at least from within a controller?

A: 

I think that the session key is stored in a variable called ENV_SESSION_KEY

PanosJee
+2  A: 

To access the name of the session cookie from within the view or the controller, you can say:

request.session_options[:session_key]

and then to access the raw value of that cookie, being an empty array if it's not set, you use:

request.cookies[ request.session_options[:session_key] ]

The cookie name ( aka session_key ) is set in your config/environment.rb file.

  config.action_controller.session = {     
    :session_key => '_project_session',
    :secret      => 'long-secret-key'
  }
austinfromboston
Hmmm, it doesn't seem to work. I'm using Rails 2.3.2, and the request.session_options hash contains a key called :key rather than :session_key. And it's set to "_session_id" rather than "_myapp_session" as it should be... lol
jimeh
+5  A: 

I found the solution. In Rails 2.3.2 at least the session key in set in config/initializers/session_store.rb like this:

ActionController::Base.session = {
  :key         => '_myapp_session',
  :secret      => '[...]'
}

And you can read the value like this:

ActionController::Base.session_options[:key]

From Base.session to Base.session_options automagically, doesn't make much sense, and it caused me a big headache... lol

jimeh
Thanks, i've spent half an hour trying to work out where this was
Marc Roberts
A: 

Note also this bug which affects tests around session_options in some versions of Rails 2.x: https://rails.lighthouseapp.com/projects/8994/tickets/2303-testrequest-doesnt-initialize-session_options

Andy Triggs