-1

好的,所以在过去一周左右的时间里,我一直在处理进行 api 调用,然后将响应返回为 JSON,然后将我想要的部分输出到我的视图中(我认为到达那里,尽管速度很慢)。我学习曲线的下一部分是获取这些数据并将其保存到模型中,以便我可以在我的应用程序的其他地方使用它。

我想要实现的是在发出 api 请求并显示结果之后我想单击一个按钮,然后将数据发布到我的模型

在这个例子中,我通过它的 ISBN 号从 itunes api 获取书籍数据

这是返回数据的示例

 {
 "resultCount":1,
  "results": [
 {"kind":"ebook", "artistId":545975179, "artistName":"Gareth Halfacree", "price":9.99, 
 "description":"<p><b>Make the most out of the world&rsquo;s first truly compact  computer<\/b><\/p><p>It's the size of a credit card, it can be charged like a smartphone,  it runs on open-source Linux, and it holds the promise of bringing programming and playing  to millions at low cost. And now you can learn how to use this amazing computer from its co-creator, Eben Upton, in <i>Raspberry Pi User Guide<\/i>. Cowritten with Gareth Halfacree, this guide gets you up and running on Raspberry Pi, whether you're an educator, hacker, hobbyist, or kid. Learn how to connect your Pi to other hardware, install software, write basic programs, and set it up to run robots, multimedia centers, and more.<\/p><ul><li>Gets you up and running on Raspberry Pi, a high-tech computer the size of a credit card <\/li><li>Helps educators teach students how to program <\/li><li>Covers connecting Raspberry Pi to other hardware, such as monitors and keyboards, how to install software, and how to configure Raspberry Pi <\/li><li>Shows you how to set up Raspberry Pi as a simple productivity computer, write basic programs in Python, connect to servos and sensors, and drive a robot or multimedia center <\/li><\/ul><p>Adults, kids, and devoted hardware hackers, now that you've got a Raspberry Pi, get the very most out of it with <i>Raspberry Pi User Guide<\/i>.<\/p>", "genreIds":["10017", "38", "9027"], "releaseDate":"2012-08-30T07:00:00Z", "currency":"USD", "genres":["Computers", "Books", "Computers & Internet"], "trackId":559783692, "trackName":"Raspberry Pi User Guide",  "artistIds":[545975179],  "artworkUrl60":"http://a2.mzstatic.com/us/r30/Publication/v4/ba/a8/2c/baa82ce0-2ac7-7026-04da-6f74bc97b403/9781118464496.60x60-50.jpg", "artistViewUrl":"https://itunes.apple.com/us/artist/gareth-halfacree/id545975179?mt=11&uo=4", "trackCensoredName":"Raspberry Pi User Guide", "formattedPrice":"$9.99", "artworkUrl100":"http://a4.mzstatic.com/us/r30/Publication/v4/ba/a8/2c/baa82ce0-2ac7-7026-04da-6f74bc97b403/9781118464496.100x100-75.jpg", "trackViewUrl":"https://itunes.apple.com/us/book/raspberry-pi-user-guide/id559783692?mt=11&uo=4", "averageUserRating":2.5, "userRatingCount":5}]
}

我想保存艺术家姓名、描述、图像(我需要回形针吗?)和曲目名称

任何人都可以就我如何解决这个问题提供一些建议,显然我创建了一个模型并设置了表列(我知道列名可以是任何东西?)但在这之后我有点迷失了

如果有人可以提供一个很好的示例,那么我可以按照这个过程并了解正在发生的事情

任何帮助表示赞赏

4

1 回答 1

2

一种选择是before_filter在您的控制器上创建一个,您可以列出您希望它执行的控制器操作,即。您的 API 调用。

在从 before 过滤器调用的方法中,您可以将请求的详细信息保存到模型中。这将避免手动保存信息的需要。

基于您的示例响应的图像具有图像的 URL,因此您可以将其保存到模型中,假设您确信图像 URL 不会更改。如果您想保存图像本身,是的,我会推荐回形针或载波之类的东西。

如果您不想这样做,那么另一种选择,在我看来不太整洁的方式是使用按钮将 JSON 发布回执行我上面提到的控制器操作。

编辑:所以要保存到您的模型,例如称为 ApiCall,您可以在每次点击控制器操作时创建一个新的数据库条目。假设您的控制器操作被调用get_info,并且它传递了使您能够构建所描述的 JSON 响应的参数。您可以执行以下操作。

json_response = JSON.parse(your_response_object)
ApiCall.create(:artist_name => json_response["results"]["artistName"])

您显然可以包含您拥有的任何/所有信息。或者,您可以将整个响应作为 YAML 字符串存储在一个 db 属性中,并在检索时解析信息,由您决定。

yaml = your_response_object.to_yaml
ApiCall.create(:payload => yaml)
于 2013-01-02T15:53:23.103 回答