3

I haven't found a good answer to this yet. How can I get my Rails app and Sinatra app (mounted in my Rails app's config.ru) to share a session successfully? If I visit my Sinatra app first, then the Rails app, I get an error like undefined method sweep for {}:Hash, presumably because Rails uses a custom subclass of Hash for storing session info, and Rack::Session::Cookie doesn't. My code so far:

config.ru

map "/" do
  run MyRailsApp::Application
end

map "/sinatra" do
  use Rack::Session::Cookie, 
      key: "_app_session",
      secret: "<SECRET_KEY>"

  run MySinatraApp
end

config/initializers/session_store.rb

MyRailsApp::Application.config.session_store :cookie_store, key: '_app_session'

config/initializers/secret_token.rb

MyRailsApp::Application.config.secret_token = "<SECRET_KEY>" # same as config.ru

Anything I've missed?

5
  • Are you using rack-flash (or some other flash implementation) in the Sinatra app?
    – matt
    Feb 4, 2012 at 1:25
  • I'm using sinatra-flash, yeah. Feb 4, 2012 at 3:22
  • 2
    I think what's happening is sinatra-flash is adding a (normal, Ruby) hash to the session under the "flash" key. Rails expects whatever's under that key to be an ActionDispatch::Flash::FlashHash object, and tries to call the sweep method on it, hence the error undefined method sweep for {}:Hash. A possible work around might be to use a different key in the Sinatra app for the flash rather than the default (e.g. flash(:my_flash)[:error]="foo"). That won't help if you want to use the flash to see messages when going between Rails and Sinatra though.
    – matt
    Feb 4, 2012 at 3:44
  • That would definitely be okay - I don't plan on passing flash messages around. I'll try that and see if that fixes things. Feb 4, 2012 at 4:13
  • Yep, that was definitely it. Thanks a ton. If you make an answer I can mark it as right. Feb 7, 2012 at 19:12

2 Answers 2

4

A quick grep of the Rails source reveals sweep is a method on ActionDispatch::Flash::FlashHash, which Rails stores in the session under the flash key.

Sinatra-Flash also uses the flash key of the session, but it stores a plain Hash object there.

Rails is getting the object at session['flash'], which is the Hash put there by Sinatra, assuming it is a FlashHash and trying to call sweep on it, hence the error message: undefined method sweep for {}:Hash.

A possible work around might be to use a different key in the Sinatra app for the flash rather than the default (e.g. flash(:my_flash)[:error]="foo").

That won't help if you want to use the flash to see messages when going between Rails and Sinatra though.

0
0

The "undefined method sweep for {}:Hash" issue is with your browser storing previously cached cookies. Just delete the cookies related to the application and refresh the browser. It worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.