Ruby on rails 如何学习访客定位轨道

Ruby on rails 如何学习访客定位轨道,ruby-on-rails,ruby,Ruby On Rails,Ruby,我使用RubyonRails 4.0.1 我需要开发链接短期服务,所以我需要找到我的用户的位置。例如英国、土耳其、比利时 如何在Rails中做到这一点?使用GeoCoder Gem。它允许您查找用户的位置 Geocoder是Ruby的完整地理编码解决方案。Rails增加了地理编码(按街道或IP地址)、反向地理编码(根据给定坐标查找街道地址)和距离查询。这很简单,只需在对象上调用地理代码,然后使用像Venue.near(“Billings,MT”)这样的范围 .我使用来自的数据从其IP地址查找访问

我使用RubyonRails 4.0.1

我需要开发链接短期服务,所以我需要找到我的用户的位置。例如英国、土耳其、比利时


如何在Rails中做到这一点?

使用GeoCoder Gem。它允许您查找用户的位置

Geocoder是Ruby的完整地理编码解决方案。Rails增加了地理编码(按街道或IP地址)、反向地理编码(根据给定坐标查找街道地址)和距离查询。这很简单,只需在对象上调用地理代码,然后使用像Venue.near(“Billings,MT”)这样的范围

.

我使用来自的数据从其IP地址查找访问者的国家。您可以从该站点下载一个文件来创建和填充表
ip2nation
。接下来,我为这种类型创建了一个模型:

require 'ipaddr'    

# 2 columns: ip and country
class Ip2nation < ActiveRecord::Base  
  self.table_name = 'ip2nation'
  self.primary_key = :ip

  #-------------  Finder

  # Get the country code for the given ip string
  def self.find_country(ip)

    # Convert the ip to a number
    ip_number = IPAddr.new(ip).to_i

    # Find the country in the database
    where('ip < ?', ip_number).order(:ip).pluck(:country).last     
  end 
end
country = Ip2nation.find_country(request.remote_ip)