0

Rails 4.2.0 と Ruby 2.2.0 を使用しています。geokit を使用して、住所を緯度と経度に変換したいと考えています。私はすでに自分の端末に gem install geokit を実行しました。また、Gemfile に gem 'geokit' を含め、bundle install を実行しました。

コントローラーで、関数 Geokit::Geocoders::GoogleGeocoder.geocode() を使用しようとしました。

このように使用すると、次のエラーが発生しました: uninitialized constant AnswersController::Geocoders (私のコントローラーは Answers と呼ばれます)

コントローラーの先頭に 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
4

2 に答える 2

1

私はすでに自分の端末に gem install geokit を実行しました。また、Gemfile に gem 'geokit' を含め、bundle install を実行しました。

両方を行う必要はありません。

次の手順に従って、geokitgem が正しくインストールされていることを確認します。

a. geokitGemfile に gem を追加する

# Gemfile
gem 'geokit'

b. 実行します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"
于 2015-04-04T16:43:52.017 に答える