1

应用程序.rb:

require 'sinatra'

class MyApp < Sinatra::Application
    enable :sessions

    if session[:user_id].nil? then
        erb :login
    end
end

require_relative 'routes/init.rb'

配置.ru:

require './app'
run MyApp

运行shotgun config.ruthin start -R config.ru产生:

app.rb:3:in `<top (required)>': undefined local variable or method `session' for main:Object (NameError)

这是非常 101ish。是什么赋予了?

4

1 回答 1

2

get我认为问题在于您在块或类似内容中没有该代码。基本上,sinatra 只会session在请求的上下文中知道对象。像这样的东西适用于根 url:

require 'sinatra'

class MyApp < Sinatra::Application
  enable :sessions

  get '/' do
    if session[:user_id].nil? then
      erb :login
    end
  end
end
于 2012-05-07T21:51:40.130 回答