views:

717

answers:

3

I would like to access the Rails session secret programmatically (I am using it to generate a sign-on token).

Here's what I've come up with:

ActionController::Base.session.first[:secret]

This returns the session secret. However, every time you call ActionController::Base.session it adds another entry to an array so you end up with something like this:

[{:session_key=>"_new_app_session", :secret=>"totally-secret-you-guys"}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

This strikes me as being no good.

Is there a better way to access the session secret?

+2  A: 
ActionController::Base.session_options_for(request,params[:action])[:secret]
whoisjake
+4  A: 

Thanks, Jake.

Since the secret doesn't change based on the request or the action, this also works:

ActionController::Base.session_options_for(nil,nil)[:secret]
Luke Francl
+1  A: 

For Rails 2.3, I've used:

ActionController::Base.session_options[:secret]
Cool, that looks cleaner. I'll have to update our code when we upgrade.
Luke Francl