地理编码器 Gem - Ruby On Rails

发布于 2024-12-10 18:24:46 字数 1042 浏览 3 评论 0原文

我刚刚将 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/

I just included the Geocoder gem into my app. 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

 @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. Since i am using geocoder and i realize that on their website they auto detect my IP, City and Country. 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

http://www.rubygeocoder.com/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追我者格杀勿论 2024-12-17 18:24:46

您可以使用 geoip gem。

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

You can use geoip gem.

Download GeoIP.dat.gz from http://www.maxmind.com/app/geolitecountry. unzip the file. The below assumes under #{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
爱要勇敢去追 2024-12-17 18:24:46

也许 GeoIP 是您的替代方案:

https://github.com/cjheath/geoip

它非常简单。我对地理编码器不太有信心,但如果您确实想使用它,您可能会观看处理它的railscast。

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

Rails 编码女性,该死的性感!^ ^

Maybe GeoIP is an alternaternative for you:

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.

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

Rails coding women, damn hot!^^

蓝色星空 2024-12-17 18:24:46

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

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

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

Yes, you can use Geocoder to geocode IP address. Geocoder would adds a location methods to Request so you just need to:

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

That works for me. Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文