我正在尝试在测试和生产中运行我的 Ruby Sinatra 应用程序。这是主要课程:
class Main < Sinatra::Application
helpers Sinatra::ContentFor
helpers Sinatra::Partials
helpers Sinatra::Auth
use Rack::Session::Cookie, :secret => 'supersecret' , :expire_after => 360000
set :environment, :production
configure :development do
enable :sessions, :logging, :dump_errors, :inline_templates
enable :methodoverride
set :root, $_APP_PATH
set :static, true
logger = Logger.new($stdout)
end
configure :production do
enable :logging, :dump_errors, :inline_templates
enable :methodoverride
set :root, $_APP_PATH
set :static, true
logger = Logger.new($stdout)
end
get "/2configure" do
haml :'2configure'
end
end
这适用于set :environment, :development
,但在“测试”和“生产”中会出现以下错误:
!! Unexpected error while processing request: undefined method `bytesize' for nil:NilClass
这是堆栈跟踪的顶部:
NoMethodError - undefined method `bytesize' for nil:NilClass:
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/utils.rb:291:in `bytesize'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/utils.rb:351:in `secure_compare'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/cookie.rb:115:in `unpacked_cookie_data'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/cookie.rb:105:in `extract_session_id'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/abstract/id.rb:43:in `load_session_id!'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/abstract/id.rb:32:in `[]'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/abstract/id.rb:252:in `current_session_id'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/abstract/id.rb:258:in `session_exists?'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/abstract/id.rb:104:in `exists?'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/abstract/id.rb:114:in `load_for_read!'
/home/id833541/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.3.10/lib/rack/session/abstract/id.rb:59:in `[]'
我几乎可以肯定这与会话有关。出于某种原因,在 Sinatra 中正确处理直肠是一种痛苦。我之前遇到过这个问题,但后来我通过使用enable :sessions
. 现在即使这样也行不通了。
我已经确认访问会话时发生错误。在 HAML 文件中,我有:
%p="Welcome #{session[:full_name]}"
第一次是空的。但这应该给出错误吗?
任何请求都会发生这种情况,即使在我不尝试访问session
对象但没有堆栈跟踪的请求中也是如此。
我正在使用瘦网络服务器。