Question

I'm trying to setup my rails server so that only U.S. users are able to take a survey on the site. To do this I installed the GeoIP gem and downloaded the country file from maxmind.com.

I'm able to create a new GeoIP object with an IP address in the rails console. But when I do an actual request in the controller it gives this error:

No such file or directory - GeoCity.dat

Here's my controller:

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

I just have the GeoIP.dat file unzipped and sitting in my main rails directory. I'm thinking that I need to load that into a database or something of the like? Thanks in advance!

Was it helpful?

Solution

Assuming that GeoCity.dat is in main Rails root directory. Use as below:

    c = GeoIP.new("#{Rails.root}/GeoCity.dat").country(request.remote_ip)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top