2015-01-09 2 views
0

로그인 한 사용자가 5 마일 이내에있는 다른 사용자의 목록을 볼 수 있지만 결과를 표시하는 보석을 가져올 수없는 지오 코드 보석을 사용하려고합니다. 보석 설명서에 나와 있듯이 테이블에 경도와 위도 열이 있으며 모든 테스트 사용자는 이러한 속성을 가지고 있으며 서로 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 %> 
+0

을 내 컨트롤러를 변경 -'current_user.nearbys (500)'. 또한 레일 콘솔에서이 결과를 확인하십시오. 내 말은 콘솔에서 테스트 해보고 결과가 괜찮다는 것을 알게되면 뷰를 표시합니다. –

+0

무슨 뜻입니까? 그 코드를 어디에 넣으시겠습니까? – ssingh

답변

0

그냥 알아냅니다. 내 모델에 다음과 같은 방법을 추가 :

def geocentric 
    User.near([latitude, longitude], 10) 
end 

을하고`current_user`을 가지고 있기 때문에 당신이 할 수없는, 따라서

def index 
    @users = current_user.geocentric.order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page]) 
end