googleplex
googleplex

Reputation: 1

How to geocode "current_user_sign_in_ip" after log in

So currently I have an application that is hosted on heroku.

I am using the devise gem for all user authentication

I want the user to be able to check their current location on a map and for this i am using the geocoder gem

However, After logging in the users ip is registered but their longitude and latitude values do not change and remain "nil".

If i go into my rails console and give the current_sign_up_ip a different value then it starts to work.

This is what my User.rb file looks like at the moment:

class User < ApplicationRecord
     attr_accessor :firstname, :lastname

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
   devise :database_authenticatable, :registerable, :recoverable,
      :rememberable, :trackable, :validatable,
      :omniauthable, :omniauth_providers => [:facebook , :google_oauth2]

  geocoded_by :current_sign_in_ip
  after_validation :geocode

   def self.from_omniauth(auth)
     where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
       user.provider = auth.provider
       user.uid = auth.uid
       user.email = auth.info.email
       user.password = Devise.friendly_token[0,20]
       user.firstname = auth.info.firstname
     end
  end
end

And this is what my view that displays the map looks like at the moment:

<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<% if user_signed_in? %>
  <div style="'width: 800px">
   <div id = "map" style = "width: 800px; height: 400px;"></div>
  </div>


<script type="application/javascript">



handler = Gmaps.build('Google'); 
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers([
    {
      "lat": (<%= current_user.latitude %>),
      "lng": (<%= current_user.longitude %>),
      "picture": {
        "url": 
"https://i.pinimg.com/736x/4e/e8/fb/4ee8fbf4312a171c9e344abb30c65e21.jpg",
       "width":  32,
       "height": 32
  },
  "infowindow": "hello!"
}
  ]);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
    });
   </script>




<% else %>

<% end %>

So the problem I am having is that evne though the "current_sign_in_ip" has a value the longitude and latitude of the user at point is not being geocoded.

It works if I get the user to enter their current IP address while logging in

Upvotes: 0

Views: 130

Answers (1)

bo-oz
bo-oz

Reputation: 2872

I checked the docs of Geocoder gem, this seems to be the correct way to configure this. I think you need to debug the after_validation method, by adding a different after_validation that tests the geocode method, something like:

after_validation :geocode_test

def geocode_test
  binding.pry # or debug if you don't use pry
  geocode
end

There should be some reason it is not working correctly. It might be in your server log as well, have you checked it for errors?

Upvotes: 0

Related Questions