Access Rails 3 Session from Rack?
I was able to do the following in Rails 2.3.5 to access attributes that I set on the session from within my Rails app. Now in Rails 3, env["rack.session"]
is nil
. How do I do the same thing in Rails 3?
class CallbackFilter
def initialize(app)
@app = app
end
def call(env)
unless env["rack.ses开发者_开发技巧sion"][:oauth_callback_method].blank?
env["REQUEST_METHOD"] = env["rack.session"].delete(:oauth_callback_method).to_s.upcase
end
@app.call(env)
end
end
It was because I placed the use CallbackFilter
in config.ru
. It should be placed in config/application.rb
like so:
config.middleware.use CallbackFilter
Otherwise the environments didn't look like they were in sync...
There's another 'dirty' way to syncronize(for those who can't integrate rack app in rails for some reasons).
You should set :key and :secret to same values in both Rails and Rack.
In rails :secret
assigned as ChatApp::Application.config.secret_token
and usually configured in initializers/secret_token.rb , and in session_store.rb there's is a :key
option for YourApp::Application.config.session_store
). So in the end it will be something like:
in Rack::Builder.new
block:
use Rack::Session::Cookie, :key => '_your_app_session',
:path => '/',
:secret => 'secret_more_than_30_dig'
initializers/session_store.rb
YourApp::Application.config.session_store :cookie_store, :key => '_your_app_session',
:path => '/'
initializers/secret_token.rb
YourApp::Application.config.secret_token = 'secret_more_than_30_dig'
now you should be able to access it throw request.env['rack.session']
精彩评论