【问题标题】:Application crashes with TypeError - nil can't be coerced into Float应用程序因 TypeError 崩溃 - 不能将 nil 强制转换为 Float
【发布时间】:2013-04-10 03:38:47
【问题描述】:

我对编程非常陌生,刚尝试将我的第一个项目放到网上,却遇到了内部服务器错误。我什至不知道从哪里开始,我在 Sinatra 上运行 Ruby 并将其托管在 Heroku 上

http://clearli.herokuapp.com/

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

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

【问题讨论】:

  • 请贴出app.rb的出处
  • 错误告诉你“nil 不能被强制转换成 Float:”的问题。您正在使用 nil 值比较或进行一些数学运算。让我们检查你的代码。
  • 感谢您修复我的代码,我不知道如何开始下一段代码。

标签: ruby heroku sinatra


【解决方案1】:

app.rb 的第 35 行声明:

@temp_difference = (@current_temp - @yesterday_temp)

错误意味着这些临时之一是nil。 AFAIU,在本地机器上你的项目运行没有任何问题,对吧?因此,问题在于获得预测。你会重新尝试获取预测,直到它最终产生?实际上我不知道Forecast::IO#forecast 应该返回什么,但它似乎部分正常(因为没有错误获取像yesterday.currently.temperature 这样的成员)虽然没有充满正确的数据(因为返回的温度是@987654326 @.)

希望对你有帮助。

【讨论】:

  • 因此,由于某种原因,该网站刚刚重新开始工作。我使用 forecast.io API 来填充“@current_temp”和“@yesterday_temp”变量。然后我使用地理编码器 gem 对用户 IP 进行地理定位,对于 API 的请求,这些值是“@lat”和“@long”。为什么页面刚刚又开始工作了?
  • 看起来像是以不公平的方式从比赛中获得预测的网站。如果您希望您的网站立即运行,您将检查它是否返回正确的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多