地理编码器 Gem - Ruby On Rails
我刚刚将 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
这个想法
是检测不是来自英国的文章。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 geoip gem。
从 http://www.maxmind.com/app/geolitecountry 下载 GeoIP.dat.gz。解压缩文件。下面假设在#{RAILS_ROOT}/db 目录下。
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 是您的替代方案:
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!^^
是的,您可以使用 Geocoder 对 IP 地址进行地理编码。地理编码器会向请求添加一个位置方法,因此您只需要:
这对我有用。希望有帮助。
Yes, you can use Geocoder to geocode IP address. Geocoder would adds a location methods to Request so you just need to:
That works for me. Hope it helps.