我试图写一个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'常量中。 –