【问题标题】:Can't use Geocoder class in my controller无法在我的控制器中使用 Geocoder 类
【发布时间】:2013-01-25 02:11:58
【问题描述】:

我有这样的麻烦:我想使用 ajax 请求来获取用户位置,所以我在我的控制器中创建了动作并在 json 中呈现 Geocoder 函数的结果。这是代码:

  def find_location
   location = Geocoder.coordinates(params[:location])
   render :json => (location)
  end

在本地效果很好,在 heroku 上也可以,但是当我将此代码添加到另一个项目时 - 它显示了这个错误

   NameError in ConnectionsController#find_location

   uninitialized constant ConnectionsController::Geocoder

这很奇怪,因为 这在控制台中工作:

      Geocoder.coordinates "Ukraine"
       => [48.379433, 31.16558] 

我尝试包含 Geocoder::Model,但它不起作用。

有人可以帮我吗?

【问题讨论】:

  • 您是否将地理编码器 gem 添加到另一个项目的 Gemfile 中?
  • 当然)Bundle show gemcoder 向我展示 gemcoder
  • Typo) 当然是地理编码器。我可以在我的 rails 控制台中使用它,所以它确实存在于我的 gemfile 中。
  • location = ::Geocoder.coordinates(params[:location]) 可以代替当前的location = Geocoder.coordinates(params[:location]) 工作吗?
  • 当我将您的第一个变体放入我的控制器时 - 我收到错误 uninitialized constant Geocoder。在本地主机中,我使用了第二个变体。

标签: ruby-on-rails module geolocation rails-geocoder


【解决方案1】:

确保您的 geocoder gem 未分组在 :development 中,或 :test 未分组在 Gemfile

它应该在组之外,以便我可以在所有环境中访问

例如:

#Gemfile

group :development, :test do
  #your gems
end 

gem 'geocoder'

【讨论】:

  • 它不在组内并且在 mongoid gem/ 之后
  • 如果它正在开发而不是在生产中,那么它一定是我的加载问题。你是否也在你的生产环境中完成了捆绑安装?
  • 是的,一个运行包安装了几次。我看到了这个问题 - github.com/alexreisner/geocoder/issues/259 - 在对 application.rb 文件进行更改后我应该重新启动服务器吗? (添加需要'geocoder/models/mongoid')
  • 是的,在生产模式下,你做任何改变后你必须重启服务器,触摸tmp/restart.txt
【解决方案2】:

基于this issue,您必须重新启动整个生产机器,而不仅仅是您的 apache 或 nginx。

我实际上不得不打开一个问题,因为它对我不起作用。 https://github.com/alexreisner/geocoder/issues/501

【讨论】:

    【解决方案3】:

    您是否有机会将 Docker 用于您的后端代码?

    如果这样做,那么在安装任何 gem 之后,您需要停止 Rails 容器,构建(如果您使用 dockerhub,则从 dockerhub 下拉)新映像,然后重新启动容器。

    一切都好?

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多