मेरी रेल अनुप्रयोग के डेटाबेस में लॉगिन स्थान सहेजा जा रहा है

वोट
0

जब मैं आवेदन में प्रवेश करें, नीचे दिए गए कोड को बुलाया जाता है। तो SessionsController अंदर, SignupHistory तालिका के साथ आबादी वाले हो जाता है .createविधि।

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  helper_method :current_user
  before_action :set_timezone, :current_country

  def current_country
    if session[:ip_country_code].present?
      return @current_country = session[:ip_country_code]
    end
    use_default = request.location.nil? || request.location.country_code.blank? || request.location.country_code == 'RD'
    country_code = use_default ? ENV['DEFAULT_IP_COUNTRY_CODE'] : request.location.country_code
    @current_country = session[:ip_country_code] = country_code
  end
end

sessions_controller.rb

class SessionsController < ApplicationController
  def save_signup_history(member_id)
    SignupHistory.create(
      member_id: member_id,
      ip: request.ip,
      accept_language: request.headers[Accept-Language],
      ua: request.headers[User-Agent],
      login_location: request.location
    )
  end
end

डेटाबेस गुण

databse

हालांकि, रेखा के बजाय login_location: request.locationआईपी जैसे डेटाबेस में साइन इन करने का स्थान लिखने के लिए New Yorkमैं क्या डेटाबेस में मिलता है,:

! --- गहरे लाल रंग का / वस्तु: जियोकोडर :: परिणाम :: Ipstack डेटा: आईपी: 127.0.0.1 COUNTRY_NAME: आरक्षित COUNTRY_CODE: आरडी cache_hit:

मैं कैसे आईपी मेरी डेटाबेस में साइन इन करते समय के आधार पर स्थान बचाने के लिए करते हैं

11/09/2018 को 18:25
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
1

आप उपयोग कर सकते हैं request.remote_ipआईपी पते पाने के लिए। डीबी में सहेजा IP पते का स्थान पाने के लिए आपको मुक्त एपीआई सेवाओं है कि आईपी पर आधारित स्थान जानकारी लाने से एक का उपयोग कर सकते हैं:
- http://ip-api.com/
- https://www.iplocation.net/
- आदि..

class SessionsController < ApplicationController

  require 'net/http'
  require 'json'

  def save_signup_history(member_id)
    SignupHistory.create(
        member_id: member_id,
        ip: request.ip,
        accept_language: request.headers["Accept-Language"],
        ua: request.headers["User-Agent"],
        login_location: get_address(request.remote_ip)
    )
  end


#http://ip-api.com/json/208.80.152.201
  def get_address(ip)
    url = "http://ip-api.com/json/#{ip}"
    uri = URI(url)
    response = Net::HTTP.get(uri)
    result = JSON.parse(response)
    result["regionName"] # returns region name 
  end
end

JSON प्रतिक्रिया:

{
"as":"AS14907 Wikimedia Foundation, Inc.",
"city":"San Francisco (South Beach)",
"country":"United States",
"countryCode":"US",
"isp":"Wikimedia Foundation, Inc.",
"lat":37.787,
"lon":-122.4,
"org":"Wikimedia Foundation, Inc.",
"query":"208.80.152.201",
"region":"",
"regionName":"California",
"status":"success",
"timezone":"America/Los_Angeles",
"zip":"94105"
}

संदर्भ:
https://apidock.com/rails/ActionController/Request/remote_ip

11/09/2018 को 21:10
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more