简体   繁体   English

Geocoder Gem - Ruby On Rails

[英]Geocoder Gem - Ruby On Rails

I just included the Geocoder gem into my app. 我刚刚将Geocoder gem添加到我的应用程序中。 Everything works perfect but i was wondering if i can user the gem even further. 一切都很完美,但我想知道我是否可以进一步使用宝石。

My application has got users and they are allowed to add articles. 我的应用程序有用户,他们可以添加文章。 At the moment i can their IP address using 目前我可以使用他们的IP地址

 @article.ip_address = request.remote_ip

I have looked for a gem which can help me convert that IP address to country name but i can't find anything. 我找了一个宝石,可以帮助我将该IP地址转换为国名,但我找不到任何东西。 Since i am using geocoder and i realize that on their website they auto detect my IP, City and Country. 由于我使用地理编码器,我意识到在他们的网站上他们自动检测我的IP,城市和国家。 I was wondering how i can implement this to my controller. 我想知道如何将其实现给我的控制器。

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

end 结束

The idea is to detect articles which are not from the UK. 这个想法是检测不是来自英国的文章。

https://github.com/alexreisner/geocoder https://github.com/alexreisner/geocoder

http://www.rubygeocoder.com/ http://www.rubygeocoder.com/

You can use geoip gem. 你可以使用geoip gem。

Download GeoIP.dat.gz from http://www.maxmind.com/app/geolitecountry . http://www.maxmind.com/app/geolitecountry下载GeoIP.dat.gz。 unzip the file. 解压缩文件。 The below assumes under #{RAILS_ROOT}/db dir. 以下假设在#{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

Yes, you can use Geocoder to geocode IP address. 是的,您可以使用Geocoder对IP地址进行地理编码。 Geocoder would adds a location methods to Request so you just need to: Geocoder会为Request添加位置方法,因此您只需:

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

That works for me. 这对我行得通。 Hope it helps. 希望能帮助到你。

Maybe GeoIP is an alternaternative for you: 也许GeoIP可以替代你:

https://github.com/cjheath/geoip https://github.com/cjheath/geoip

Its really simple. 它真的很简单。 Im not that confident to geocoder but if you definitfly want to use it you might watch the railscast that deals with it. 我对地理编码器没有那么自信,但如果你definitfly想要使用它,你可能会看到处理它的railscast。

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

Rails coding women, damn hot!^^ Rails编码女人,真该死!^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM