Ruby on rails Rails数组排序依据在Rails控制台中工作,但在控制器中不工作,抛出“浮点与浮点比较失败”

Ruby on rails Rails数组排序依据在Rails控制台中工作,但在控制器中不工作,抛出“浮点与浮点比较失败”,ruby-on-rails,ruby,params,Ruby On Rails,Ruby,Params,在我的控制器中,我有以下功能: @destinations = Location.all.where("current_forecast like ?", params[:weather]) @destinations.sort_by{ |d| d.distance_to([params[:latitude], params[:longitude]]) }.first 这将抛出浮点与浮点失败错误的比较。但是,如果我在控制台中输入以下内容: @destinations = Location

在我的控制器中,我有以下功能:

@destinations = Location.all.where("current_forecast like ?", params[:weather])

@destinations.sort_by{ |d|
  d.distance_to([params[:latitude], params[:longitude]])
}.first
这将抛出浮点与浮点失败错误的比较。但是,如果我在控制台中输入以下内容:

@destinations = Location.all.where("current_forecast like ?", params[:weather])

@destinations.sort_by{ |d|
  d.distance_to([34, -112])
}
我不明白这个错误。为什么?

参数散列中的值是字符串,需要转换为浮点数。答案只是将距离_的参数更改为浮动:

  d.distance_to([params[:latitude].to_f, params[:longitude].to_f])