2011-10-20 9 views
4

私はちょうど私のアプリにジオコーダーの宝石を含んでいました。すべてが完璧に動作しますが、私は宝石をさらに利用できるかどうか疑問に思っていました。Geocoder Gem - Ruby On Rails

私のアプリケーションにはユーザーがいて、記事を追加することはできます。

@article.ip_address = request.remote_ip 

を使用して私ができる自分のIPアドレス現時点では私は私が国の名にそのIPアドレスを変換することができますが、私は何かを見つけることができません宝石を探しています。私はジオコーダを使用しているので、彼らのウェブサイトで私のIP、都市、国を自動的に検出することがわかります。私はこれを私のコントローラーにどのように実装できるのだろうかと思っていた。

def create 
@article = Breeder.new(params[:breeder]) 
@article.user = current_user 
@article.ip_address = request.remote_ip 

respond_to do |format| 
    if @article.save 
    format.html { redirect_to @article, notice: 'Article was successfully created.' } 
    format.json { render json: @article, status: :created, location: @article } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @article.errors, status: :unprocessable_entity } 
    end 
end 

エンド

アイデアは、英国からでない記事を検出することです。

https://github.com/alexreisner/geocoder

http://www.rubygeocoder.com/

答えて

0

たぶんのGeoIPはあなたのためにalternaternativeです:

https://github.com/cjheath/geoip

その本当に簡単。あなたがジオコーダに自信を持っているわけではありませんが、もしあなたがそれを使いたいのであれば、それを扱うレールキャストを見るかもしれません。

http://railscasts.com/episodes/273-geocoder

Railsの女性をコーディング、いまいましい熱い!^^

+0

ありがとう、ありがとう。 omhhhh私はどのように上記のコードに実装するのですか?もう一度ありがとう、本当に一目惚れです。 – Benjamin

4

あなたはgeoipの宝石を使用することができます。

http://www.maxmind.com/app/geolitecountryからGeoIP.dat.gzをダウンロードしてください。ファイルを解凍します。以下は#{RAILS_ROOT}/dbディレクトリの下に置かれています。

@geoip ||= GeoIP.new("#{RAILS_ROOT}/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  
    @model.country = location_location[2] 
    end 
end 
0

はい、ジオコーダーを使用してIPアドレスをジオコードできます。 GeocoderはLocationメソッドをRequestに追加するだけで、次のようにする必要があります。

sender_ip = request.remote_ip 
sender_country = request.location.country 
sender_city = request.location.city 

これは私のために機能します。それが役に立てば幸い。

関連する問題