Reputation: 72
When current user creates a post want to know user location via IP reverse method in Geo-coder.For that i have created a post ,user and location model. I have gone through railscasts and have learn to installed Geocoder and able to find latitude, longitude via address. Now how to integrate user id from devise to geocoder to determine current user location.
class Location < ActiveRecord::Base
belongs_to :user
geocoded_by :address
after_validation :geocode, :if => :address_changed?
end
class LocationsController < ApplicationController
before_action :set_location, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#location = request.location
@locations = Location.all
respond_with(@locations)
end
def show
respond_with(@location)
end
def new
@location = Location.new
respond_with(@location)
end
def edit
end
def create
if params[:location].blank?
@location = request.location
@locations = Location.near([current_user.latitude, current_user.longitude], 50, :order => :distance)
end
respond_with(@location)
end
Updated:
ActiveRecord::Schema.define(version: 20150507160846) do
create_table "locations", force: true do |t|
t.string "address"
t.float "latitude"
t.float "longitude"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
t.string "integer"
end
create_table "posts", force: true do |t|
t.text "post"
t.string "location"
t.string "tag_list"
t.boolean "active"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
t.string "location"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
has_many :locations
end
Upvotes: 0
Views: 960
Reputation: 5112
Based on the discussion ,if You want to get the users ip
and then use it to get the address
...you can use the below code in user.rb
with ip
already present in users
table.
Devise stores the current and last ip address of the user automagically in user table. The column names it uses are "current_sign_in_ip", "last_sign_in_ip".
Given a User model with known IP address, automatically fetch coordinates and store in latitude and longitude attributes:
# app/models/user.rb
geocoded_by :current_sign_in_ip,
:latitude => :lat, :longitude => :lon
after_validation :geocode
Upvotes: 2