Heroku 上的未定义方法(使用 Puma)
Undefined method on Heroku (using Puma)
我们在 Rails 上使用带有 Ruby 的 Postgis 适配器,我们正尝试在 Heroku 上使用 Puma 作为我们的生产环境。我们的测试在开发系统上完美通过。此外,该服务器在除 Heroku 之外的所有生产服务器上都能完美运行,但是一旦我们 运行 在 Heroku + Puma(或 Unicron)上运行它,我们就会遇到异常。
附带说明一下,当在 Heroku 上使用 Webrick 部署时,该应用程序可以顺利运行。
当使用 puma(稍后尝试使用 unicorn)作为默认 Web 服务器时,一切顺利,但有一件事:
我正在使用 RGeo gem 来跟踪一些蜂巢位置。当切换到 Puma (Unicorn) 时,在我的 Javascript 视图中调用 @hive.current_location.lon
和 @hive.current_location.lat
会引发错误:
ActionView::Template::Error (undefined method 'lon' and 'lat' for #<RGeo::Cartesian::PointImpl:0x007f700846c970>)
同时,我尝试通过heroku run rails console
访问current_location经纬度
有趣的是,在 Heroku 控制台上获取纬度和经度值 returns 正确的值。看来是puma和unicorn的问题。
有什么建议吗?
我终于找到了解决办法。阅读此 post。
更多详情:
基本上,在我的蜂巢模型中我使用:
set_rgeo_factory_for_column(:current_location,
RGeo::Geographic.spherical_factory(:srid => 4326))
然后在我的位置创建器中,我有以下代码:
require_relative "./coordinate_builder"
require 'rgeo'
class LocationCreator
DEFAULT = "default"
def self.create(lat_lng)
return nil if lat_lng == DEFAULT
lng, lat = CoordinateBuilder.parse(lat_lng)
geographic_factory = RGeo::Geographic.spherical_factory
geographic_factory.point(lng, lat)
end
end
稍后在视图中,我使用以下代码获取点的纬度和经度,以便创建 Google 地图标记并将其固定在地图上:
<% if @hive.current_location.present? %>
var myLatlng = new google.maps.LatLng(<%= @hive.current_location.latitude %>, <%=@hive.current_location.longitude %>);
var mapOptions = {
zoom: 16,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
<% end %>
问题:显然 Rgeo::Geographic::SphericalPointImpl
falls back to RGeo::Cartesian::PointImpl
in my Javascript code.
解决方法:我将 javascript 调用从 @hive.current_location.lat
更改为 @hive.current_location.y
。同样对于纬度,我也做了同样的事情:@hive.current_location.lon
到 @hive.current_location.x
.
这解决了我的问题。
我们在 Rails 上使用带有 Ruby 的 Postgis 适配器,我们正尝试在 Heroku 上使用 Puma 作为我们的生产环境。我们的测试在开发系统上完美通过。此外,该服务器在除 Heroku 之外的所有生产服务器上都能完美运行,但是一旦我们 运行 在 Heroku + Puma(或 Unicron)上运行它,我们就会遇到异常。
附带说明一下,当在 Heroku 上使用 Webrick 部署时,该应用程序可以顺利运行。
当使用 puma(稍后尝试使用 unicorn)作为默认 Web 服务器时,一切顺利,但有一件事:
我正在使用 RGeo gem 来跟踪一些蜂巢位置。当切换到 Puma (Unicorn) 时,在我的 Javascript 视图中调用 @hive.current_location.lon
和 @hive.current_location.lat
会引发错误:
ActionView::Template::Error (undefined method 'lon' and 'lat' for #<RGeo::Cartesian::PointImpl:0x007f700846c970>)
同时,我尝试通过heroku run rails console
有趣的是,在 Heroku 控制台上获取纬度和经度值 returns 正确的值。看来是puma和unicorn的问题。
有什么建议吗?
我终于找到了解决办法。阅读此 post。
更多详情:
基本上,在我的蜂巢模型中我使用:
set_rgeo_factory_for_column(:current_location,
RGeo::Geographic.spherical_factory(:srid => 4326))
然后在我的位置创建器中,我有以下代码:
require_relative "./coordinate_builder"
require 'rgeo'
class LocationCreator
DEFAULT = "default"
def self.create(lat_lng)
return nil if lat_lng == DEFAULT
lng, lat = CoordinateBuilder.parse(lat_lng)
geographic_factory = RGeo::Geographic.spherical_factory
geographic_factory.point(lng, lat)
end
end
稍后在视图中,我使用以下代码获取点的纬度和经度,以便创建 Google 地图标记并将其固定在地图上:
<% if @hive.current_location.present? %>
var myLatlng = new google.maps.LatLng(<%= @hive.current_location.latitude %>, <%=@hive.current_location.longitude %>);
var mapOptions = {
zoom: 16,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
<% end %>
问题:显然 Rgeo::Geographic::SphericalPointImpl
falls back to RGeo::Cartesian::PointImpl
in my Javascript code.
解决方法:我将 javascript 调用从 @hive.current_location.lat
更改为 @hive.current_location.y
。同样对于纬度,我也做了同样的事情:@hive.current_location.lon
到 @hive.current_location.x
.
这解决了我的问题。