我正在使用Rails 4.2.0和Ruby2.2.0。我想用geokit把地址转换成经纬度。我已经在我的终端上安装了geokit。我还在我的Gemfile中包含了gem 'geokit‘,并运行了bundle安装。
在控制器上,我尝试使用函数Geokit::Geocoders::GoogleGeocoder.geocode()。
当我像这样使用它时,我得到了一个错误:未初始化的常量应答控制器::Geocoders(我的控制器称为答案)
当我试图在控制器的开头添加require 'geokit‘时,我得到了一个错误:无法加载这样的文件-geokit。
你知道我能做些什么来解决这个问题吗?
提前感谢
这是我的控制器
require 'rubygems'
require 'geokit'
class AnswersController < ApplicationController
before_action :set_answer, only: [:show, :edit, :update, :destroy]
def render_page2
render('page2')
end
def answer_page2
if params[:address_origin] && params[:address_destination]
session[:lat_origin] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_origin]).lat
session[:lon_origin] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_origin]).lng
session[:lat_destination] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_destination]).lat
session[:lon_destination] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_destination]).lng
#session[:lat_origin] = 37.793688
#session[:lon_origin] = -122.3958692
#session[:lat_destination] = 37.866437
#session[:lon_destination] = -122.265415
redirect_to '/page3'
else
flash[:message] = "All the questions are required on this page."
redirect_to '/page2'
end
end
end发布于 2015-04-04 17:03:35
我想我找到了解决办法。代码很好,Prakash发布的所有步骤都很好。
但是我需要在运行这些步骤之后在终端上重新启动rails服务器,这就是为什么它不能在浏览器上工作的原因!现在起作用了。
谢谢大家的回答。我不知道这是否是最好的解决办法,但至少有效。
发布于 2015-04-04 16:43:52
我已经在我的终端上安装了geokit。我还在我的Gemfile中包含了gem 'geokit‘,并运行了bundle安装。
没必要两者兼得。
按照以下步骤确保正确安装了geokit创业板:
a.在Gemfile中添加geokit宝石
# Gemfile
gem 'geokit'运行bundle install;验证gem是否安装了bundle show geokit。
c.在rails控制台中尝试以下操作:
$ rails console
> a=Geokit::Geocoders::GoogleGeocoder.geocode '140 Market St, San Francisco, CA'
=> #<Geokit::GeoLoc:0x007fe877305c70 @all=[#<Geokit::GeoLoc:0x007fe877305c70 ...>], @street_address="140 Market Street", @sub_premise=nil, @street_number="140", @street_name="Market Street", @city="San Francisco", @state=nil, @state_code="CA", @state_name="California", @zip="94105", @country_code="US", @province="CA", @success=true, @precision="address", @full_address="140 Market Street, San Francisco, CA 94105, USA", @lat=37.793688, @lng=-122.3958692, @provider="google", @neighborhood="Financial District", @district="San Francisco County", @country="United States", @accuracy=8, @suggested_bounds=#<Geokit::Bounds:0x007fe8772fef60 @sw=#<Geokit::LatLng:0x007fe8772fef88 @lat=37.7923338697085, @lng=-122.3972116302915>, @ne=#<Geokit::LatLng:0x007fe8772ff050 @lat=37.7950318302915, @lng=-122.3945136697085>>>
> a.ll
=> "37.793688,-122.3958692"https://stackoverflow.com/questions/29448769
复制相似问题