铁轨 + Sinatra 应用程序共享会话
我还没有找到一个好的答案。如何让我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 源代码的快速
grep
显示sweep
是ActionDispatch::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 revealssweep
is a method onActionDispatch::Flash::FlashHash
, which Rails stores in the session under theflash
key.Sinatra-Flash also uses the
flash
key of the session, but it stores a plainHash
object there.Rails is getting the object at
session['flash']
, which is theHash
put there by Sinatra, assuming it is aFlashHash
and trying to callsweep
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.
“未定义的方法扫描 {}: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.