Accessing a Rails 3 session from a rack?

I was able to do the following in Rails 2.3.5 to access the attributes I set in session from my Rails application. Rails 3 now env["rack.session"]

has nil

. How do I do the same in Rails 3?

class CallbackFilter
  def initialize(app)
    @app = app
  end

  def call(env)
    unless env["rack.session"][:oauth_callback_method].blank?
      env["REQUEST_METHOD"] = env["rack.session"].delete(:oauth_callback_method).to_s.upcase
    end
    @app.call(env)
  end
end

      

+2


a source to share


2 answers


This is because I placed use CallbackFilter

in config.ru

. It should be placed in the config/application.rb

following way:

config.middleware.use CallbackFilter

      



Otherwise the environments didn't look like they were in sync ...

+4


a source


There is another "dirty" way to sync (for those who cannot integrate the rack application into the rails for some reason).

You must set: key and: secret to be the same in Rails and Rack.

In rails :secret

assigned as ChatApp::Application.config.secret_token

normally configured in initializers / secret_token.rb and session_store.rb has an option :key

for YourApp::Application.config.session_store

). So in the end it will be something like:

in Rack::Builder.new

:

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'

      

you should now be able to access it throw request.env['rack.session']

+1


a source







All Articles