我正在尝试在测试和生产中运行我的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
,但在"test"one_answers"production"中,它给出以下错误:
!! 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 `[]'
我几乎可以肯定这与会话有关。出于某种原因,在辛纳屈的表演中,这是一种痛苦。我以前有过这个问题,但后来我用enable :sessions
使它工作。现在就连这也行不通了。
我已经确认在访问会话时发生错误。在一个HAML文件中,我有:
%p="Welcome #{session[:full_name]}"
第一次为空。但这会产生错误吗?
它发生在任何请求中,即使在我不尝试访问session
对象的请求中,但是没有堆栈跟踪。
我使用的是Thin web服务器
我相信这是因为你也有use Rack::Session::Cookie
和enable :sessions
。使用其中之一(我不知道为什么不能同时工作,只知道它是其中之一)。
顺便说一下,你试过用https://rubygems.org/gems/encrypted_cookie代替Cookie吗?这样更安全。
快速补充:我注意到你继承了Sinatra::Application
。如果你想通过继承来创建一个模块化的应用,那么:
require 'sinatra/base'
class Main < Sinatra::Base
如果你想使用它作为一个经典的应用程序,那么只需:
require 'sinatra'
# no `class` statement
get "/blah" do # etc
继续注释:
如果你正在使用一个机架文件和机架中间件,那么这就是我如何构建一个应用程序:
root_of_project/
config.ru # This is just to start up the app via `rackup`.
app/ # This will be the `root` setting in Sinatra
config.rb # This is where the logic of the Rack app lives.
main.rb # This is where your main Sinatra app lives
models/
views/
2configure.haml
public/
config.ru
require 'rubygems'
require 'bundler'
Bundler.setup(:default) # and anything else you want added in
root = File.expand_path File.dirname(__FILE__)
require File.join( root , "./app/config.rb" )
map "/" do
run MY_APP_NAMESPACE.app
end
app/config.rb
require_relative "./main.rb" # Sinatra app
module MY_APP_NAMESPACE
require 'encrypted_cookie'
# standard cookie settings
COOKIE_SETTINGS = {
:path => "/",
:expire_after => 86400, # In seconds, 1 day
:secret => ENV["COOKIE_KEY"], # I load this into the server's environment.
:httponly => true
}
# This gets called in the config.ru. Doing it this way
# makes it easy to include in specs.
def self.app
Rack::Builder.app do
logger = Logger.new($stdout)
cookie_settings = COOKIE_SETTINGS
# more security if in production
cookie_settings.merge!( :secure => true ) if ENV["RACK_ENV"] == "production"
# AES encryption of cookies
use Rack::Session::EncryptedCookie, cookie_settings
# any other Rack middleware I put here.
run Main
end
end
end # module
app/main.rb
require 'sinatra/base'
module MY_APP_NAMESPACE
class Main < Sinatra::Base
helpers Sinatra::ContentFor
helpers Sinatra::Partials
helpers Sinatra::Auth
# no need for this here
# use Rack::Session::Cookie, :secret => 'supersecret' , :expire_after => 360000
# no need for this
# set :environment, :production
configure :development do
#enable :sessions, # no need
enable :logging, :dump_errors, :inline_templates
enable :methodoverride
# Sinatra will work this out
# set :root, $_APP_PATH
# set :static, true # no need if you have a public directory
end
configure :production do
enable :dump_errors, :inline_templates
enable :methodoverride
# set :root, $_APP_PATH
# set :static, true
# logger = Logger.new($stdout) # no need for this, as far as I remember
# see http://www.sinatrarb.com/intro#Logging
end
get "/2configure" do
haml :'2configure'
end
end
希望这能给你一些尝试的想法
显然问题是在Rack::Session::Cookie
某处。现在我可以使用下面的代码:
use Rack::Session::Pool, :secret => 'super secret' , :expire_after => 360000
但是我的应用程序稍后将与当前使用Rack::Session::Cookie
的另一个应用程序集成。
我仍然想知道为什么Cookie
不工作