【问题标题】:No such file or directory - GeoCity.dat Rails没有这样的文件或目录 - GeoCity.dat Rails
【发布时间】:2014-03-24 19:06:42
【问题描述】:

我正在尝试设置我的 rails 服务器,以便只有美国用户能够在网站上进行调查。为此,我安装了GeoIP gem 并从 maxmind.com 下载了国家/地区文件。

我可以在 rails 控制台中创建一个带有 IP 地址的新 GeoIP 对象。但是当我在控制器中执行实际请求时,它会出现此错误:

没有这样的文件或目录 - GeoCity.dat

这是我的控制器:

module Api
  module V1
    class SurveysController < ApplicationController
      before_filter :check_us

      def survey
        @survey = Survey.check_survey params[:user_auth_token]
        redirect_to api_v1_survey_path(@survey)
      end

      private

      def check_us
        c = GeoIP.new('GeoCity.dat').country(request.remote_ip)
        head :unauthorized unless c.country_code2 = "US"
      end
    end
  end
end

我只是将 GeoIP.dat 文件解压缩并放在我的主要 rails 目录中。我在想我需要将它加载到数据库或类似的东西中?提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby geoip


    【解决方案1】:

    假设GeoCity.dat 在 Rails 主根目录中。如下使用:

        c = GeoIP.new("#{Rails.root}/GeoCity.dat").country(request.remote_ip)
    

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 2013-03-31
      • 1970-01-01
      • 2014-10-23
      • 2021-06-24
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多