Sinatra应用程序在heroku上抛出内部服务器错误

时间:2012-09-29 17:33:57

标签: ruby heroku timezone sinatra internal-server-error

我在heroku上部署了一个小的sinatra应用程序。我尝试访问该页面时收到内部服务器错误消息。这是我从heroku logs

得到的
2012-09-29T17:24:18+00:00 app[web.1]: TypeError - can't convert nil into an exact number:
2012-09-29T17:24:18+00:00 app[web.1]:   /app/birthday_countdown.rb:9:in `+'
2012-09-29T17:24:18+00:00 app[web.1]:   web.rb:5:in `block in <main>'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `block in compile!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `[]'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (2 levels) in route!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `process_route'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/birthday_countdown.rb:9:in `initialize'
2012-09-29T17:24:18+00:00 app[web.1]:   web.rb:5:in `new'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `each'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:936:in `dispatch!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:872:in `block in process_route'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:835:in `block (3 levels) in route!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:870:in `catch'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/rub
y/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:833:in `route!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:834:in `block in route!'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:129:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:99:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.4.1/lib/thin/connection.rb:78:in `pre_process'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `block in spawn_threadpool'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.4.1/lib/thin/connection.rb:80:in `block in pre_process'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `call'
2012-09-29T17:24:18+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.4.1/lib/thin/connection.rb:78:in `catch'
2012-09-29T17:24:18+00:00 app[web.1]: 180.215.44.250 - - [29/Sep/2012 17:24:18] "GET / HTTP/1.1" 500 30 0.0012
2012-09-29T17:24:18+00:00 heroku[router]: GET debo-ka-birthday.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=8ms status=500 bytes=30

我在我的应用中进行了以下更改

@month = Time.at(Time.now.utc + Time.zone_offset('IST')).month
@day = Time.at(Time.now.utc + Time.zone_offset('IST')).day
@year = Time.at(Time.now.utc + Time.zone_offset('IST')).year

@month = Time.now.month
@day = Time.now.day
@year = Time.now.year

我之所以这样做,是因为我希望时间符合印度标准时间,而不是Heroku服务器上的时间。然而这打破了应用程序,有人可以告诉我如何修复它吗?

1 个答案:

答案 0 :(得分:2)

“IST” isn’t one of the timezones included in the time standard library,因此Time.zone_offset('IST')会返回nil

您可以使用IST的实际偏移量来代替:

Time.at(Time.now.utc + Time.zone_offset('+05:30')).month

使用localtime method

可能更容易
Time.now.localtime('+05:30')

给出相同的结果。