使用IP地址自动填写访问者所在国家/地区的表单

时间:2015-06-12 20:01:55

标签: ruby-on-rails ruby geoip freegeoip

这是我第一次这样做。我有一个新用户表单(投资者模型),我希望它是一旦表单加载后,根据访问者的IP,country字段已填充该国家/地区。我听说过geoip宝石。不知道如何使用它。这就是我试图做的事情。我从GeoIP.dat.gz下载了http://www.maxmind.com/app/geolitecountry,将其解压缩并放入我应用的db文件夹中。我不确定自己是否走在正确的道路上。

的Gemfile

source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'pg'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'

gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'foundation-rails', '~> 5.5.2.1'
gem 'foundation-icons-sass-rails'
gem 'geoip', '~> 1.5.0'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'rails_12factor', group: :production

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

的environment.rb

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!

config.gem 'geoip'

以下步骤在浏览器中显示cannot load such file -- geoip错误。我甚至无法继续尝试在表单中插入用户的国家/地区。任何帮助将不胜感激。 investors_controller.rb

require 'geoip'

class InvestorsController < ApplicationController

  @geoip ||= GeoIP.new("/db/GeoIP.dat")    
  remote_ip = request.remote_ip 
  if remote_ip != "127.0.0.1" #todo: check for other local addresses or set default value
    location_location = @geoip.country(remote_ip)
    if location_location != nil     
      @investor.country = location_location[2]
    end
  end


  def new
    @investor = Investor.new
  end

  def create
    @investor = Investor.new(user_params)
    if @investor.save
        flash.now[:success] = "Welcome, you're now on your way to success!"
        render "/static_pages/welcome"
      InvestorMailer.welcome_email(@investor).deliver_now
    else
        render 'new'
    end
  end

  def update
    if @investor.update_attributes(user_params)
        flash[:success] = "Updated"
    else
        render 'edit'
    end
  end

  private

    def user_params
      params.require(:investor).permit(:name, :email, :country,
                                   :phone)
    end
end

2 个答案:

答案 0 :(得分:1)

我在Geocoder gem取得了很大的成功。它开箱即用:

在你的Gemfile中,添加:

gem geocoder

捆绑安装后,您可以在应用中对FreeGeoIP(每小时最多10,000个)进行查询。以下是使用Google的DNS IP的控制台示例:

013 > results = Geocoder.search("8.8.4.4")
=> [#<Geocoder::Result::Freegeoip:0x007fa285fddfc0 @data={"ip"=>"8.8.4.4", "country_code"=>"US", "country_name"=>"United States", "region_code"=>"", "region_name"=>"", "city"=>"", "zip_code"=>"", "time_zone"=>"", "latitude"=>38, "longitude"=>-97, "metro_code"=>0}, @cache_hit=false>]

014 > results.first.data["country_name"]
=> "United States"

正如您所看到的,Geocoder gem提供了许多其他有用的数据。 :)

强烈 建议您阅读整个Geocoder readme,因为它会为您提供有关服务的提示和建议,如果您愿意,可以配置以执行地址查找,以及测试实践等。

我还建议,一旦您缩小了要查看的服务范围,请阅读这些服务的服务条款(例如Google地图,Bing等),因为Geocoder gem的自述文件不是&#39 ; t始终与这些政策保持同步。

答案 1 :(得分:0)

尝试添加require&#39; ruby​​gems&#39;在要求&#39; geoip&#39;

之前