0

當前用戶創建一個帖子想通過IP反向方法知道用戶位置在地理編碼器。爲此,我創建了一個帖子,用戶和位置模型。我已經通過railscasts學習安裝Geocoder並能夠通過地址查找緯度和經度。現在,如何將設計中的用戶標識集成到地理編碼器以確定當前的用戶位置。Rails 4:Geocoder與Devise

class Location < ActiveRecord::Base 
 
\t belongs_to :user 
 
\t geocoded_by :address 
 
\t 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

更新時間:

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

+0

你可以使用Geocoder.search(「1雙城路,明尼阿波利斯」)。 ... Geocoder.search(「44.981667,-93.27833」).... Geocoder.search(「204.57.220.1」)..... – Milind

+0

@Milind將Geocoder附加到用戶標識的任何方式? –

+0

你想要做什麼....如果你真的想附加user_id,那麼你可以添加地址列到用戶表......這樣,每個用戶都有自己的細節。但截至目前......你無法在不對模型實施進行更改的情況下將地理編碼詳細信息附加到user_id中。 – Milind

回答

1

根據討論,如果你想獲得用戶ip,然後用它來獲取address。 ..您可以使用中的以下代碼與ip已存在於users表中。

Devise將用戶的當前和最後一個IP地址自動存儲在用戶表中。它使用的列名是「current_sign_in_ip」,「last_sign_in_ip」。

考慮用戶模式與已知的IP地址,自動獲取座標,並存儲在緯度和經度屬性:

# app/models/user.rb 
geocoded_by :current_sign_in_ip, 
    :latitude => :lat, :longitude => :lon 
after_validation :geocode 
+0

我是新來的鐵軌,我創建了只有用戶模型和視圖,沒有控制器,當我檢查配置控制器在設計github ...我應該這樣做或其他事情..你可以幫助我的步驟指導如何去解決.....我也讀過「位置方法容易受到通過HTTP標頭的簡單IP地址欺騙」。 。所以使用geocoderv可以安全地從當前用戶獲得ip以找到他們的位置? –

+0

首先通過記錄用戶使用會話控制器或首先註冊用戶來實現devise的默認行爲,然後驗證您的db record.you將獲得當前ip hick,您可以使用進一步。嘗試它。或告訴我什麼點你卡住了。 – Milind