[英]How do I order the results of rails geocoder gem by something other than distance
[英]How can I successfully perform distance queries with geocoder gem?
我正在嘗試使用地理編碼器gem,以便已登錄的用戶可以查看位於5英里范圍內的其他用戶的列表,但似乎無法讓gem顯示結果。 如gem手冊中所述,我的表中有經度和緯度列,並且我的所有測試用戶都具有這些屬性,並且彼此之間相距5英里之內,因此這應該可以工作。 但是,我的視圖頁面(_network.html.erb)當前未顯示任何結果。 我是編碼的新手,非常感謝您的幫助。 下面的代碼,誰能告訴我我在做什么錯,以及如何解決?
users_controller
class UsersController < ApplicationController
before_filter :authenticate_user!
helper_method :sort_column, :sort_direction
def index
@users = User.near([current_user.longitude, current_user.latitude], 500).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
def show
@user = User.find(params[:id])
end
def new
end
def create
end
def edit
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:success] = "Your profile has been updated!"
redirect_to @user
else
flash[:error] = "Please try again"
redirect_to edit_profile_user_path(@user)
end
end
def destroy
end
def edit_profile
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_industry, :years_in_current_industry, :hobbies, :previous_industries, :bio, :ip_address, :latitude, :longitude, :linkedin)
end
def sender
@user = User.find(params[:id])
end
def recipient
@user = User.find(params[:id])
end
def geolocate
@user = User.find(params[:id])
if @user.updated_at < 1.day.ago || params[:force] == 'true'
if @user.update_attributes(latitude: params[:latitude].to_f, longitude: params[:longitude].to_f)
render nothing: true, status: 200
else
render nothing: true, status: 500
end
else
render nothing: true, status: 304
end
end
private
def sort_column
User.column_names.include?(params[:sort]) ? params[:sort] : "years_in_current_industry"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def geolocate_user
location = request.location
@user.update_attributes(latitude: location.latitude, longitude: location.longitude)
redirect_to :controller=>'users', :action => 'index'
end
end
User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :current_industry, :years_in_current_industry, :hobbies, :bio, :ip_address, :latitude, :longitude, :previous_industries, :linkedin
reverse_geocoded_by :latitude, :longitude
def full_name
[first_name, last_name].join(" ")
end
end
index.html.erb
<div id='user_id' data-user=<%= current_user.id %>></div>
<h1>Network</h1>
<%= submit_tag 'GEOLOCATE ME', :type => 'button', class: 'geo-button' %>
<div class ="network">
<div class="network-body">
<div id="network"><%= render 'network' %></div>
</div>
</div>
<br>
_network.html.erb
<table class="table">
<thead>
<tr>
<th>Name</th>
<th><%= sortable "current_industry", "Current Industry" %></th>
<th><%= sortable "years_in_current_industry", "Years" %></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= link_to user.full_name, user_path(user.id) %></td>
<td><%= user.current_industry %></td>
<td><%= User::EXPERIENCE[user.years_in_current_industry] %></td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate @users %>
只是弄清楚。 我在模型中添加了以下方法:
def geocentric
User.near([latitude, longitude], 10)
end
並相應地更改了我的控制器
def index
@users = current_user.geocentric.order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.