Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用geocoder gem成功执行距离查询?_Ruby On Rails_Rails Geocoder - Fatal编程技术网

Ruby on rails 如何使用geocoder gem成功执行距离查询?

Ruby on rails 如何使用geocoder gem成功执行距离查询?,ruby-on-rails,rails-geocoder,Ruby On Rails,Rails Geocoder,我正在尝试使用geocoder gem,以便登录的用户可以查看位于5英里以内的其他用户的列表,但似乎无法让gem显示结果。正如gem手册中所指出的,我的表中有经度和纬度列,我的所有测试用户都有这些属性,并且彼此之间的距离都在5英里以内,所以这应该可以正常工作。但是,我的查看页面_network.html.erb当前未显示任何结果。我对编码还不熟悉,非常感谢您的帮助。下面的代码,谁能告诉我我做错了什么,以及如何着手修复 用户/控制器 class UsersController < Appli

我正在尝试使用geocoder gem,以便登录的用户可以查看位于5英里以内的其他用户的列表,但似乎无法让gem显示结果。正如gem手册中所指出的,我的表中有经度和纬度列,我的所有测试用户都有这些属性,并且彼此之间的距离都在5英里以内,所以这应该可以正常工作。但是,我的查看页面_network.html.erb当前未显示任何结果。我对编码还不熟悉,非常感谢您的帮助。下面的代码,谁能告诉我我做错了什么,以及如何着手修复

用户/控制器

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

由于您有当前的_用户,您不能这样做-当前的_用户.nearbys500。在rails控制台上也检查一下,看看您是否得到了一些结果。我的意思是在控制台上测试它,一旦理解结果是好的,就显示在您的视图中。这是什么意思?你会把代码放在哪里?