2014-01-14 17 views
0

我想通過使用地理編碼器與當前用戶的位置有多接近來命令我的帖子。 這裏是控制器:使用地理編碼器按距離向城市排序帖子

def top 
@city = request.location.city 
@closepost = Post.near(@city, order: :distance) 
end 

這裏是視圖:

<% @closepost.each do |post| %> 
<%= post.title %> 
<% end %> 

我收到此錯誤:

undefined method `to_f' for {:order=>:distance}:Hash 
+0

不應@closepost在控制器複數? –

+0

是的,我改變了我的錯誤。現在爲{:order =>:distance}獲取未定義的方法'to_f':散列錯誤。感覺@closepost行是錯誤的,但不知道要改變它。 – user2759575

+0

謝謝,我會嘗試。 – user2759575

回答

1

在控制器中定義:

@closepost

而鑑於你在呼喚:

@closeposts

和你打電話.each上沒有定義的變量。

更新

irb> a=Geokit::Geocoders::GoogleGeocoder.geocode '140 Market St, San Francisco, CA' 
irb> a.ll 
=> 37.79363,-122.396116 

@closeposts = Post.within(5, :origin => @city.ll).order('distance DESC')  

與地理編碼

@closeposts = Post.near('dublin', 50, :order => :distance) 

你錯過了距離參數

+0

哎呀,我的壞。現在我正在爲{:order =>:distance}得到未定義的方法'to_f':散列錯誤。 – user2759575

+0

你究竟在哪裏找到了'.near(@city,order::distance)'方法?我正在查看文檔,找不到「訂單」的內容。 – rmagnum2002

+0

從另一個stackoverflow答案找到,所以我甚至不知道它是否正確。我無法找到文檔中的任何東西,從ip位置的距離排序。 – user2759575

相關問題