4

Geocoder gem をアプリに含めました。すべてが完璧に機能しますが、宝石をさらに使用できるかどうか疑問に思っていました.

私のアプリケーションにはユーザーがいて、記事を追加することが許可されています。現時点では、IPアドレスを使用できます

 @article.ip_address = request.remote_ip

そのIPアドレスを国名に変換するのに役立つgemを探しましたが、何も見つかりません. 私はジオコーダーを使用しているので、彼らのウェブサイトで私の 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/

4

3 に答える 3

4

geoip gem を使用できます。

http://www.maxmind.com/app/geolitecountryから GeoIP.dat.gz をダウンロードします。ファイルを解凍します。以下は #{RAILS_ROOT}/db dir 以下を想定しています。

@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
于 2011-10-20T18:18:25.033 に答える
0

たぶん、GeoIP があなたの代わりになるでしょう:

https://github.com/cjheath/geoip

とてもシンプルです。ジオコーダーには自信がありませんが、それを使用したい場合は、それを扱っているレールキャストを見ることができます。

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

Railsコーディングの女性たち、めちゃくちゃ熱い!^^

于 2011-10-20T15:54:06.837 に答える
0

はい、Geocoder を使用して IP アドレスをジオコーディングできます。Geocoder はロケーション メソッドを Request に追加するため、次のことを行うだけです。

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

それは私にとってはうまくいきます。それが役に立てば幸い。

于 2014-09-01T20:39:37.797 に答える