2015-06-12 81 views
1

這是我第一次這樣做。我爲新用戶提供了一個表格(投資者模式),我希望當表格被加載後,根據訪客的IP,country字段已經填寫了國家。我聽說geoip寶石。不知道如何使用它。這是我試圖做的。我從http://www.maxmind.com/app/geolitecountry下載了GeoIP.dat.gz,解壓縮到我的應用的db文件夾中。我不確定我是否在正確的道路上。使用IP地址自動填寫訪客國家的表格

的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 
+0

確保在將geoip添加到Gemfile後運行'bundle install'。 – infused

+0

這是我做的第一件事。 – Emanuel

+0

那麼......真正的問題是什麼? – MarsAtomic

回答

1

我有很多與Geocoder gem成功。它的工作原理差不多吧開箱:

在你的Gemfile,添加:

gem geocoder

一旦捆綁安裝,你可以做對FreeGeoIP查詢(高達10,000每小時)就在您的應用程序。下面是從控制檯的例子,使用谷歌的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 readme因爲它會給你提示,對服務的意見,您可以配置做地址查詢,如果你願意的話,以及用於測試實踐等

我也建議,一旦你縮小了你想看的服務,閱讀這些服務的服務條款(例如谷歌地圖,必應等)作爲Geocoder gem的自述文件並不總是取決於與這些政策的日期。

0

嘗試添加要求 'RubyGems的' 前需要 'geoip的'