铁轨 + Sinatra 应用程序共享会话

发布于 2025-01-02 07:08:14 字数 858 浏览 1 评论 0原文

我还没有找到一个好的答案。如何让我的 Rails 应用程序和 Sinatra 应用程序(安装在我的 Rails 应用程序的 config.ru 中)成功共享会话?如果我先访问我的 Sinatra 应用程序,然后访问 Rails 应用程序,我会收到类似 undefined method scan for {}:Hash 的错误,大概是因为 Rails 使用 Hash 的自定义子类来存储会话信息,而 Rack ::Session::Cookie 没有。到目前为止我的代码:

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

我错过了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2025-01-09 07:08:14

Rails 源代码的快速 grep 显示 sweepActionDispatch::Flash::FlashHash 上的一个方法,Rails 将其存储在会话中的闪光键。

Sinatra-Flash 还使用会话的 flash 键,但它在那里存储一个普通的 Hash 对象。

Rails 正在 session['flash'] 处获取对象,这是 Sinatra 放在那里的 Hash,假设它是 FlashHash 并尝试对其调用 sweep,因此会出现错误消息:undefined method scan for {}:Hash

一种可能的解决方法是在 Sinatra 应用程序中为 flash 使用不同的键而不是默认键(例如 flash(:my_flash)[:error]="foo")。

不过,如果您想在 Rails 和 Sinatra 之间切换时使用 Flash 查看消息,这将无济于事。

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.

悟红尘 2025-01-09 07:08:14

“未定义的方法扫描 {}:Hash”问题是由于您的浏览器存储了之前缓存的 Cookie。只需删除与应用程序相关的cookie并刷新浏览器即可。这对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文