加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Geocoder Gem – Ruby On Rails

发布时间:2020-12-17 02:30:27 所属栏目:百科 来源:网络整理
导读:我刚刚将Geocoder gem添加到我的应用程序中.一切都很完美,但我想知道我是否可以进一步使用宝石. 我的应用程序有用户,他们可以添加文章.目前我可以使用他们的IP地址 @article.ip_address = request.remote_ip 我找了一个宝石,可以帮助我将该IP地址转换为国名,
我刚刚将Geocoder gem添加到我的应用程序中.一切都很完美,但我想知道我是否可以进一步使用宝石.

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

@article.ip_address = request.remote_ip

我找了一个宝石,可以帮助我将该IP地址转换为国名,但我找不到任何东西.由于我使用地理编码器,我意识到在他们的网站上他们自动检测我的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/

解决方法

你可以使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读