Geocoder Gem - 获得“保留"对于 request.location.country (即使在本地!) [英] Geocoder Gem - getting "Reserved" for request.location.country (even in Local!)

查看:36
本文介绍了Geocoder Gem - 获得“保留"对于 request.location.country (即使在本地!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在本地查看地理编码器国家和城市的输出,以获取诸如Yu are in {country detection} 之类的消息.

I need to be able to see locally the output of geocoder countries and cities for a message like 'Hi, Yu are in {country detected}.

为了能够在本地/开发模式下使用地理编码器,我遵循了一些 SO 建议并放置:

To be able to use geocoder in local/dev mode,I have followed some SO advice and put:

在配置/环境/development.rb

in config/environment/development.rb

class ActionDispatch::Request
  def remote_ip
    "84.34.156.155" # fake ip for example                                                                                                                                                                                                                                                                                    
  end
end

并在 application_controller 中

and in application_controller

  def lookup_ip_location
    if Rails.env.development?
      Geocoder.search(request.remote_ip).first
    else
      request.location
    end
  end

如果我在我的主页上放:

on my homepage if I put:

您位于 <%= request.location.country %>

You are in <%= request.location.country %>

它在本地显示您处于保留状态".它认为 ip = 00.00.00 并且地理编码器被构建为返回在这种情况下保留".

it displays locally "you are in Reserved". It think that ip = 00.00.00 and geocoder is built to return "reserved in that case".

很公平,如果我输入:<%= request.remote_ip %>,它就会工作并给我我在 config/environment/develomment.rb 中输入的 IP:84.34.156.155

Fair enough, if I put : <%= request.remote_ip %>, it then works and gives me the IP i have put in config/environment/develomment.rb: 84.34.156.155

所以我试图输入你在 <%= request.remote_ip.country %>",但它不起作用并给了我错误:

So i tried to put "You are in <%= request.remote_ip.country %>", but it does not work and gives me the error:

undefined method `country' for 84.34.156.155":String

如何在本地显示我设置的IP位置所在的国家!

How can I display locally the countriy of the IP location i have set !

谢谢

它在生产中运行良好(它显示你在法国"!)但在本地运行不正常!

It works fine in Production (it displays 'you are in France'!) but not locally!

我该怎么办?重要的是我可以在本地工作和检查东西,而不必每次都部署来检查它是否有效.

How can I do? It's major that i could work and check stuff locally and not have to deploy everytime to check if it works.

推荐答案

你应该称之为

Geocoder.search(request.remote_ip).first.country

这篇关于Geocoder Gem - 获得“保留"对于 request.location.country (即使在本地!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆