我正在尝试使用地理编码器gem将经纬度坐标应用于我的CoffeeShops模型,但是我无法让地理编码器返回坐标。
当我尝试使用地址保存该类的新实例时,它已成功保存到数据库,但是经度和纬度的地理编码信息均为“ nil”。
我尝试了捆绑安装。 我尝试在geocoder.rb初始化程序中添加超时 我尝试在模型中添加和删除地址验证(状态为true)
class CoffeeShop < ApplicationRecord
belongs_to :user
has_many :reviews
has_one :feature_set
has_one :wifi_speed, through: :feature_sets
validates :name, :address, presence: true
geocoded_by :address
after_validation :geocode, if: :will_save_change_to_address?
end
class AddAddressToCoffeeShops < ActiveRecord::Migration[5.2]
def change
add_column :coffee_shops, :address, :string
end
end
class AddCoordiantesToCoffeeShops < ActiveRecord::Migration[5.2]
def change
add_column :coffee_shops, :latitude, :float
add_column :coffee_shops, :longitude, :float
end
end
create_table "coffee_shops", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "rating"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id"
t.float "latitude"
t.float "longitude"
t.string "address"
t.index ["user_id"], name: "index_coffee_shops_on_user_id"
end
Geocoder.configure(
# Geocoding options
# timeout: 3, # geocoding service timeout (secs)
# lookup: :nominatim, # name of geocoding service (symbol)
# ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol)
# language: :en, # ISO-639 language code
# use_https: false, # use HTTPS for lookup requests? (if supported)
# http_proxy: nil, # HTTP proxy server (user:pass@host:port)
# https_proxy: nil, # HTTPS proxy server (user:pass@host:port)
# api_key: nil, # API key for geocoding service
# cache: nil, # cache object (must respond to #[], #[]=, and #del)
# cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys
# Exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# supports SocketError and Timeout::Error
# always_raise: [],
# Calculation options
units: :km, # :km for kilometers or :mi for miles
# distances: :linear # :spherical or :linear
)