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

时间:2016-11-22 17:41:30

标签: ruby 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路线时,我无法再访问session[:id]路线中设置的/login,它会返回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]

1 个答案:

答案 0 :(得分:0)

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