Ruby on rails Geocoder Gem-Ruby On Rails

Ruby on rails Geocoder Gem-Ruby On Rails,ruby-on-rails,ruby-on-rails-3.1,google-geocoder,Ruby On Rails,Ruby On Rails 3.1,Google Geocoder,我刚刚在我的应用程序中加入了Geocoder gem。一切都很完美,但我想知道我是否可以使用宝石更进一步 我的应用程序有用户,他们可以添加文章。目前我可以使用他们的IP地址 @article.ip_address = request.remote_ip 我一直在寻找一个可以帮助我将IP地址转换为国家名称的gem,但我什么也找不到。因为我在使用geocoder,我意识到在他们的网站上,他们会自动检测我的IP、城市和国家。我想知道如何在我的控制器上实现这一点 def create @articl

我刚刚在我的应用程序中加入了Geocoder gem。一切都很完美,但我想知道我是否可以使用宝石更进一步

我的应用程序有用户,他们可以添加文章。目前我可以使用他们的IP地址

 @article.ip_address = request.remote_ip
我一直在寻找一个可以帮助我将IP地址转换为国家名称的gem,但我什么也找不到。因为我在使用geocoder,我意识到在他们的网站上,他们会自动检测我的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
结束

其目的是检测不来自英国的物品


也许GeoIP是您的替代选择:

这真的很简单。我对地理编码器没有信心,但如果你真的想使用它,你可以看一下处理它的铁路公司

女人,他妈的性感^^

您可以使用geoip gem

从下载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

是的,您可以使用地理编码器对IP地址进行地理编码。Geocoder会在请求中添加定位方法,因此您只需:

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

这对我很有用。希望有帮助。

谢谢你的帮助,看起来很酷。omhhh如何在上面的代码上实现它。再次感谢,乍一看真的很好看。