Rails + Sinatra应用共享会话 [英] Rails + Sinatra apps sharing sessions

查看:139
本文介绍了Rails + Sinatra应用共享会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到一个很好的答案。如何获得我的Rails应用程序和Sinatra应用程序(安装在我的Rails应用程序的config.ru)成功地共享会话?如果我先访问我的Sinatra应用程序,然后Rails应用程序,我得到一个错误像未定义的方法扫描{}:Hash ,可能是因为Rails使用自定义子类Hash存储会话信息,而Rack :: Session :: Cookie则不会。我的代码到目前为止:



config.ru

  map/do 
运行MyRailsApp :: Application
end

map/ sinatrado
使用Rack :: Session :: Cookie,
key:_app_session,
secret:< SECRET_KEY>

运行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 

我错过了什么?


< Rails源中的一个快速 grep 显示扫描 ActionDispatch :: Flash :: FlashHash 上的一个方法,其中Rails存储在 flash 键下的会话中。



Sinatra-Flash也使用会话的 flash 键,但它存储一个简单的 c>



Rails正在获取对象 session ['flash'] ,它是由Sinatra放在那里的 Hash ,假设它是一个 FlashHash ,并试图调用 sweep ,因此错误消息:未定义的方法扫描{}:Hash



一个可能的解决方法是使用Sinatra应用程序中的不同的键,而不是默认值(例如 flash(:my_flash)[:error] =foo / code>)。



如果你想在Rails和Sinatra之间使用flash查看邮件,

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?

解决方案

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.

这篇关于Rails + Sinatra应用共享会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆