应用程序崩溃与TypeError - nil无法强制转换为Float

时间:2013-04-10 03:38:48

标签: ruby heroku sinatra

我对编程非常陌生,只是尝试将我的第一个项目放在网上并且出现内部服务器错误。我甚至不知道从哪里开始,我在Sinatra上运行Ruby并在Heroku上托管它

http://clearli.herokuapp.com/

这是我的ruby日志,用于向服务器发送lass请求,我正确地发布了这个问题。任何帮助将不胜感激

2013-04-10T03:30:14.733305+00:00 app[web.1]: TypeError - nil can't be coerced into Float:
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/app.rb:35:in `-'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/app.rb:35:in `block in <top (required)>'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1415:in `call'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1415:in `block in compile!'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:944:in `[]'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:944:in `block (3 levels) in route!'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:960:in `route_eval'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:944:in `block (2 levels) in route!'
2013-04-10T03:30:14.733305+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:981:in `block in process_route'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:979:in `catch'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:979:in `process_route'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:943:in `block in route!'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:942:in `each'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:942:in `route!'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1053:in `block in dispatch!'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1035:in `block in invoke'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1035:in `catch'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1035:in `invoke'
2013-04-10T03:30:14.733614+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1050:in `dispatch!'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:878:in `block in call!'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1035:in `block in invoke'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1035:in `catch'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1035:in `invoke'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:878:in `call!'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:864:in `call'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.0/lib/rack/protection/xss_header.rb:18:in `call'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.0/lib/rack/protection/path_traversal.rb:16:in `call'
2013-04-10T03:30:14.741803+00:00 heroku[router]: at=info method=GET path=/ host=clearli.herokuapp.com fwd="69.159.113.216" dyno=web.1 connect=1ms service=1474ms status=500 bytes=30
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.0/lib/rack/protection/json_csrf.rb:18:in `call'
2013-04-10T03:30:14.733865+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.0/lib/rack/protection/base.rb:49:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.5.0/lib/rack/protection/frame_options.rb:31:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:209:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:202:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:172:in `call'
2013-04-10T03:30:14.734121+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1947:in `call'
2013-04-10T03:30:14.734355+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1610:in `block in call'
2013-04-10T03:30:14.734355+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1693:in `synchronize'
2013-04-10T03:30:14.734355+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.4.2/lib/sinatra/base.rb:1610:in `call'
2013-04-10T03:30:14.734355+00:00 app[web.1]:    /app/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
2013-04-10T03:30:14.734355+00:00 app[web.1]:    /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
2013-04-10T03:30:14.734355+00:00 app[web.1]:    /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
2013-04-10T03:30:14.734355+00:00 app[web.1]:    /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
2013-04-10T03:30:14.734355+00:00 app[web.1]: 69.159.113.216 - - [10/Apr/2013 03:30:14] "GET / HTTP/1.1" 500 30 1.4439
2013-04-10T03:30:16.891604+00:00 heroku[router]: at=info method=GET path=/ host=clearli.herokuapp.com fwd="69.159.113.216" dyno=web.1 connect=1ms service=1268ms status=500 bytes=30

app.rb

require 'rubygems'
require 'sinatra'
require 'haml'
require 'hashie'
require 'forecast_io'
require 'shotgun'
require 'geocoder'

Geocoder.configure(:timeout => 60)

Forecast::IO.configure do |configuration|
  configuration.api_key = 'fe6fa0b5700a6134723fabdb8a08b296'
end

get '/' do

    result = request.location
    @lat = result.latitude
    @long = result.longitude

    city = request.location.city
    @city = city

    forecast = Forecast::IO.forecast(@lat, @long)
    @current_temp = forecast.currently.temperature
    @current_wind = (forecast.currently.windSpeed)
    @current_icon = forecast.currently.icon
    @current_clouds = forecast.currently.cloudCover

    yesterday = Forecast::IO.forecast(@lat, @long, time: (Time.new.to_i - 86400))
    @yesterday_temp = yesterday.currently.temperature
    @yesterday_wind = (yesterday.currently.windSpeed)
    @yesterday_clouds = yesterday.currently.cloudCover

    @temp_difference = (@current_temp - @yesterday_temp)
    @temp_difference_abs = @temp_difference.abs 
    @wind_difference = (((@current_wind - @yesterday_wind)/@yesterday_wind)*100)
    @cloud_difference = (((@current_clouds - @yesterday_clouds)/@yesterday_clouds)*100)

    @temp_description = "warmer"
        if @temp_difference < 0
            @temp_description = "colder"
        end

    @wind_description = "you dont need to worry about the wind"
        if (@wind_difference >= 0 && @wind_difference <= 50)
            @wind_description = "a bit windier"
        elsif (@wind_difference > 50 && @wind_difference <= 100)
            @wind_description = "noticeably windier"
        elsif (@wind_difference > 100)
            @wind_description = "much more windy"
        elsif (@wind_difference <= 0 && @wind_difference >= -50)
            @wind_description = "a bit less windy"
        elsif (@wind_difference < -50 && @wind_difference >= -100)
            @wind_description = "noticeably less windy"
        elsif (@wind_difference < -100)
            @wind_description = "way less windy"
        end

    @cloud_description = "no brighter or cloudier than"
        if (@cloud_difference >= 0 && @cloud_difference <= 25)
            @cloud_description = "a bit cloudier than"
        elsif (@cloud_difference > 25 && @cloud_difference <= 75)
            @cloud_description = "much more cloudy than"
        elsif (@cloud_difference > 75)
            @cloud_description = "disgustingly cloudy compared to"
        elsif (@cloud_difference < 0 && @cloud_difference >= -25)
            @cloud_description = "a bit clearer than"
        elsif (@cloud_difference < -25 && @cloud_difference >= -75)
            @cloud_description = "way clearer than"
        elsif (@cloud_difference < -75)
            @cloud_description = "incredibly clear compared to" 
        end

    @unit = "degrees"
    if @temp_difference == 1 || @temp_difference == -1
        @unit = "degree"
    end 
    haml :index
end

1 个答案:

答案 0 :(得分:1)

app.rb的第35行说明:

@temp_difference = (@current_temp - @yesterday_temp)

错误表示其中一个临时值为nil。 AFAIU,在本地机器上你的项目运行没有任何问题,对吧?因此问题在于获得预测。您是否会重新获得预测,直到它最终屈服?实际上我不知道Forecast::IO#forecast应该返回什么,但似乎部分正常(因为yesterday.currently.temperature等成员没有错误,但是没有充分的正确数据(因为温度)返回的是nil。)

希望它有所帮助。