session[:id] 变量未在来自 sinatra 的另一条路线中被拾取

session[:id] variable not being picked up within another route from sinatra

我正在按照这里的教程设置用户登录 https://learn.co/lessons/sinatra-user-auth

它说为了在我的路线中保存 session[:id] 我会做这样的事情:

post '/login' do
  @user = User.get_id(params['email'],params['password'])
  session[:id] = @user # equals a numeric value of the matching user
  redirect '/home'
end

然而,当我到达 /home 路线时,我无法再访问 /login 路线中设置的 session[:id],它 returns nil

# this doesnt return the session id
# consequently I cant lookup my user records
get '/home' do
  @user = User.object(session[:id]) # looks up user object with numeric value
  erb :home
end

我试过在我的配置控制器中启用会话,但也没有用

module Controllers

  class ApplicationController < Sinatra::Base

    # . . . .

    configure :production, :development do
      enable :sessions, :logging 
    end

  end 

end 

如何让我的应用在路由之间保持 session[:id]

在您的 sinatra 应用程序中需要您的 sinatra gem 后,您是否尝试过启用 :sessions?不在您的控制器中