1

这是我第一次这样做。我有一个新用户表格(投资者模型),我希望一旦加载表格,根据访问者的 IP,该country字段已经填写了国家。我听说过geoip宝石。不知道怎么用。这就是我试图做的。我GeoIP.dat.gz从下载http://www.maxmind.com/app/geolitecountry,提取它并将其放入db我的应用程序的文件夹中。我不确定我是否走在正确的道路上。

宝石文件

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

环境.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
4

2 回答 2

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 自述文件,因为它会为您提供提示、有关您可以配置以根据需要进行地址查找的服务的建议,以及测试实践等。

我还建议您缩小要查看的服务范围后,阅读这些服务的服务条款(例如 Google Maps、Bing 等),因为 Geocoder gem 的自述文件并不总是与那些政策。

于 2015-06-16T17:55:14.390 回答
0

尝试在 require 'geoip' 之前添加 require 'rubygems'

于 2015-06-12T21:22:19.513 回答