2015-04-04 19 views
0

我正在使用Rails 4.2.0和Ruby 2.2.0。我想使用geokit將地址轉換爲經度和緯度。我已經在我的終端上安裝了gemit。我還在gemfile中加入了gem'geokit',並運行bundle install。如何在rails項目上使用geokit

在控制器上,我嘗試使用函數Geokit :: Geocoders :: GoogleGeocoder.geocode()。

當我使用它,這樣,我得到這個錯誤:未初始化不斷AnswersController ::地址解析器(我的控制器被稱爲答案)

當我嘗試添加要求「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 
+0

向我們展示你的代碼在'answers_controller.rb'中。 – Victor 2015-04-04 16:32:43

回答

1

我想我找到了解決方案。代碼很好,Prakash發佈的所有步驟都很好。

但是我需要在運行這些步驟後重啓終端上的rails服務器,這就是爲什麼它不能在瀏覽器上工作!現在它正在工作。

謝謝大家的回答。我不知道這是否是最好的解決方案,但至少它是有效的。

1

我已經做寶石安裝geokit在我的終端。我還將Gem 'geokit'包含在我的Gemfile中,並運行bundle install。

不需要兩者兼而有之。

按照這些步驟,以確保你已經正確安裝了geokit寶石:

一個。添加geokit寶石文件中的寶石

# Gemfile 
gem 'geokit' 

b。運行bundle install;驗證寶石是否安裝了bundle show geokit

c。在軌道控制檯上試試以下內容:

$ 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" 
+0

是的,在控制檯中工作 – saraf 2015-04-04 16:46:48

+0

我只是不知道如何在我的項目中使用它。我在運行rails server時遇到了這些錯誤,並使用localhost在瀏覽器上啓動了我的應用程序 – saraf 2015-04-04 16:47:55

相關問題