我試圖寫一個ActiveResources模式與谷歌Places API的互動 - https://developers.google.com/places/web-service/search#PlaceSearchRequests谷歌地方的ActiveResource
我看到了API需要密鑰,並以獲取結果的一些參數。所以我的問題是:我該如何正確地將API密鑰添加到請求中,因爲現在我不知道它是否實際上正在通過,以及我如何設置模型以便在稍後想要傳遞參數時傳遞參數這樣做:
GooglePlace.where(query: 'whatever')
我到目前爲止只是一個的ActiveResource默認樣板,沒有任何線索,如何添加缺少的信息:
class GooglePlace < ActiveResource::Base
self.site = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=pool'
@key = API_KEY
end
而且我的索引操作方法在控制器看起來像這樣:
def index
@users = GooglePlace.where(query: 'pool')
end
在控制檯中,我得到的是這樣的:
Started GET "/google_places" for ::1 at 2016-02-27 16:41:11 -0300
Processing by GooglePlacesController#index as HTML
Rendered google_places/index.html.erb within layouts/application (61.0ms)
Completed 500 Internal Server Error in 2027ms (ActiveRecord: 0.0ms)
任何幫助將非常感激。感謝您的時間!
您是否爲應用程序獲取API密鑰? – Baronz
我嘗試使用ActiveResource,發現它如此絕望,我最終編寫了自己的API訪問庫。對你不是很有幫助,但我感到你的痛苦。所以,一個想法是使用'webmock'來測試你的'GooglePlace'類。然後,你就可以看到你的'GooglePlace'類正在發送什麼 - 正文,標題等等。祝你好運! – jvillian
@Baronz:是的,我已經有鑰匙了,它在'API_KEY'常量中。 –