當前用戶創建一個帖子想通過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
你可以使用Geocoder.search(「1雙城路,明尼阿波利斯」)。 ... Geocoder.search(「44.981667,-93.27833」).... Geocoder.search(「204.57.220.1」)..... – Milind
@Milind將Geocoder附加到用戶標識的任何方式? –
你想要做什麼....如果你真的想附加user_id,那麼你可以添加地址列到用戶表......這樣,每個用戶都有自己的細節。但截至目前......你無法在不對模型實施進行更改的情況下將地理編碼詳細信息附加到user_id中。 – Milind