如何将这些方法链接在一起(Geocoder + Pagination)

时间:2015-03-30 14:37:54

标签: ruby-on-rails pagination rails-geocoder

我目前使用geocoder gem有一系列位置:

@locations = Location.near([@lat, @lon], 3)

产生:

=> [#<Location id: 18, address: "555 N 7th Ave, Jamaica, CA  66666", phone: "(623) 555-5555", latitude: 66.666, longitude: -200.082, user_id:  1, created_at: "2015-03-11 15:06:57", updated_at: "2015-03-11 15:06:57">,

#<Location id: 46, address: "555 N 7th Ave, Jamaica, CA  66666", phone: "(623) 555-5555", latitude: 66.666, longitude: -200.082, user_id: 1, created_at: "2015-03-11 15:06:57", updated_at: "2015-03-11 15:06:57">]

我试图用

为这些结果添加分页
@locations = Location.near([@lat, @lon], 3).paginate(:page => params[:page], :per_page => 5)

并在视图中(haml):

= will_paginate @locations

但我收到以下错误:

ActionView::Template::Error (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.457341299999996 - locations.lati' at line 1: SELECT COUNT(locations.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.457341299999996 - locations.latitude) * PI() / 180 / 2), 2) + COS(33.457341299999996 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((-112.07359500000001 - locations.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((locations.longitude - -112.07359500000001) / 57.2957795), ((locations.latitude - 33.457341299999996) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing) FROM `locations`  WHERE (locations.latitude BETWEEN 33.41392176506674 AND 33.50076083493325 AND locations.longitude BETWEEN -112.12563828858949 AND -112.02155171141054 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((33.457341299999996 - locations.latitude) * PI() / 180 / 2), 2) + COS(33.457341299999996 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((-112.07359500000001 - locations.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 3)):

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试在模板中单独渲染@locations?这是我通常使用的模式:

= will_paginate
render @locations
= will_paginate

这至少可以帮助您解决问题。