Это первый раз, когда я это делаю. У меня есть форма для новых пользователей (модель инвестора), и я хочу, чтобы после загрузки формы, на основе IP-адреса посетителя, поле country
уже заполнено страной. Я слышал о камне geoip
. Не знаю, как его использовать. Это то, что я пытался сделать. Я загрузил GeoIP.dat.gz
из http://www.maxmind.com/app/geolitecountry
, извлек его и поместил в папку db
моего приложения. Я не уверен, что я на правильном пути.Автозаполнение формы с страны посетителя с использованием IP-адреса
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'pg'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'foundation-rails', '~> 5.5.2.1'
gem 'foundation-icons-sass-rails'
gem 'geoip', '~> 1.5.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'rails_12factor', group: :production
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
environment.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
config.gem 'geoip'
Следующие шаги показывает cannot load such file -- geoip
ошибку в браузере. И я даже не могу двигаться дальше, пытаясь интерполировать страну пользователя в форме. Любая помощь будет оценена. investors_controller.rb
require 'geoip'
class InvestorsController < ApplicationController
@geoip ||= GeoIP.new("/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
@investor.country = location_location[2]
end
end
def new
@investor = Investor.new
end
def create
@investor = Investor.new(user_params)
if @investor.save
flash.now[:success] = "Welcome, you're now on your way to success!"
render "/static_pages/welcome"
InvestorMailer.welcome_email(@investor).deliver_now
else
render 'new'
end
end
def update
if @investor.update_attributes(user_params)
flash[:success] = "Updated"
else
render 'edit'
end
end
private
def user_params
params.require(:investor).permit(:name, :email, :country,
:phone)
end
end
Убедитесь, что вы запустили 'bundle install' после добавления geoip в свой Gemfile. – infused
это было первое, что я сделал. – Emanuel
Итак ... каков вопрос? – MarsAtomic