简体   繁体   English

用geokit导轨过滤-剂量显示任何数据

[英]Filtering with geokit rails - dosent show any data

I am building an API for a webbapp using Ruby On Rails, i am using Jbuilder for API/JSON and Geokit. 我正在使用Ruby On Rails构建用于webbapp的API,我正在将Jbuilder用于API / JSON和Geokit。

I have this models: 我有这个模型:

Campaign 运动

class Campaign < ActiveRecord::Base
    belongs_to :company
    belongs_to :venue
    belongs_to :category
    has_and_belongs_to_many :venues
    has_many :cities, through: :venues
    has_many :uses
end

Company 公司

class Company < ActiveRecord::Base
  has_many :venues
  has_many :campaigns
  validates :name, presence: true
end

Venue 会场

class Venue < ActiveRecord::Base

    acts_as_mappable :default_units => :kms,
                     :lat_column_name => :lat,
                     :lng_column_name => :lng

    belongs_to :company
    belongs_to :city
    has_and_belongs_to_many :campaigns

end

I am trying to list all campaigns by given latitude and longitude coordinates, like this: 我正在尝试按给定的纬度和经度坐标列出所有广告系列,如下所示:

class API::CampaignsController < ApplicationController
    respond_to :json

    def index
        @campaigns = filter
        respond_with @campaigns
    end
    def filter
        if params[:city_id]
            Campaign.includes(:venues, :cities).where('cities.id' => params[:city_id])
        elsif params[:lat] && params[:lng]
            lat = params[:lat].to_f
            lng = params[:lng].to_f
            Campaign.includes(:venues, :cities).within(10, :origin => [lat, lng])
        end
    end

end

But all I get is a empty hash: 但是我得到的只是一个空哈希:

{
campaigns: [ ]
}

Any ides on what I am doing wrong and how I can fix this? 是否对我做错了什么以及如何解决此问题有任何看法?

As per the server log, GET "/api/campaigns?lat=55.563466?lng=12.973509" , you are not passing params[:city_id] so your BOTH conditions in filter method will fail and no query would be executed. 按照服务器日志, GET "/api/campaigns?lat=55.563466?lng=12.973509" ,你是不是通过params[:city_id]让你在这两个条件下filter方法将失败,并没有查询将被执行。 Hence, empty campaigns . 因此,空campaigns

acts_as_mappable is on Venue model so within is available only for that model. acts_as_mappableVenue模型上,因此within仅适用于该模型。 I think you want filter method as below: 我认为您想要的filter方法如下:

  def filter
    if params[:city_id]
      Campaign.includes(:venues, :cities).where('cities.id' => params[:city_id])
    elsif params[:lat] && params[:lng]
      lat = params[:lat].to_f
      lng = params[:lng].to_f
      Venue.within(10, :origin => [lat, lng]).includes(:campaigns, :city).each do |venue|
        campaigns << venue.campaigns  
      end
      campaigns
    end
  end

Also, the url should be /api/campaigns?lat=55.563466&lng=12.973509 . 此外,网址应为/api/campaigns?lat=55.563466&lng=12.973509

NOTICE: 注意:

Pass lng as a separate query param with & prefix as &lng and not as ?lng lng作为单独的query param传递, &前缀为&lng而不是?lng

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM