2012-05-10 77 views
0

我使用geocoder和设计宝石。我试图获得接近用户的位置,优惠券设计宝石和Geocoder宝石

优惠券模式

def index 
    if params[:search].present? 
    @coupons = Coupon.near([user_long, user_lat], 50, :order => :distance) 
    else 
    @coupons = Coupon.all 
    end 
end 

应用助手

我已经定义了user_long和user_lat

def user_long 
      current_user.longitude 
    end 


    def user_lat 
      current_user.latitude 
    end 

设计宝石

我试图用色器件宝石帮手得到像这样

优惠券模式

def index 
    if params[:search].present? 
    @coupons = Coupon.near([current_user.longitude, current_user.latitude], 50, :order => :distance) 
    else 
    @coupons = Coupon.all 
    end 
end 

我打的墙壁以及与此吊顶的值。有人可以帮忙,我知道这是新手问题,但我无法解决它,所以拯救我的生命?

+0

目前发生了什么? – flooooo

+0

@flooooo此刻,没有任何显示,并且我的日志中没有错误。 – Benjamin

+0

@Benjamin你是怎么得到current_user.lat和long?我曾尝试添加一个after_validation:geocode到:current_sign_in_ip,但它似乎没有工作.. –

回答

0

我的错误是我在那个纬度之前有经度,它拿走了逻辑

这工作。

def index 
    @coupons = Coupon.near([current_user.latitude, current_user.longitude], 50, :order => :distance) 

end 
0

你是不是索引网页上看到任何东西,因为你的@coupons数组为空:

@coupons = Coupon.near([current_user.longitude, current_user.latitude], 50, :order => :distance) 

在开发日志(在轨运行服务器,如果从运行轨道服务器在同一窗口控制台),您应该查看为CouponsController#index操作生成的SQL查询。

假设你确定你这样的 '近' 查询:

class Coupon 
    scope :near, lambda { |longitude, latitude| some logic... 
end 

您可以使用 “轨道控制台” 调试这个 '近' 的方法是这样的:

rails console 
> Coupon.near(10, 20) 

等。

+0

感谢您的帮助。让我再次从新鲜的眼睛看它。我按照你的建议使用了控制台,我意识到我的问题是顺序或纬度和经度。 – Benjamin