-1

さて、先週かそこらで、私はapi呼び出しを行い、応答をJSONとして返し、ビューに表示したい部分を出力することに取り組んできました(ゆっくりではありますが)。学習曲線の次の部分は、このデータを取得してモデルに保存し、アプリの他の場所で使用できるようにすることです。

私が達成したいのは、APIリクエストが行われ、結果が表示された後です。ボタンをクリックして、データをモデルに投稿します。

この例では、iTunesAPIからISBN番号を介して書籍のデータを取得しています。

これが返されるデータの例です

 {
 "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}]
}

アーティスト名、説明、画像(これにはペーパークリップが必要ですか?)、trackNameを保存したいと思います

誰かが私がこれについてどうやって行くかについていくつかのアドバイスを提供できますか、明らかに私はモデルを作成してテーブルの列を設定します(列名は何でもかまいませんか?)しかしこの後私は少し失われました

誰かが私がプロセスに従って何が起こっているのかを理解することができるので素晴らしい例を提供することができれば

助けていただければ幸いです

4

1 に答える 1

2

1つのオプションはbefore_filter、コントローラー上にを作成することです。つまり、実行するコントローラーアクションを一覧表示できます。API呼び出し。

beforeフィルターから呼び出されたメソッド内で、リクエストの詳細をモデルに保存できます。これにより、情報を手動で保存する必要がなくなります。

応答例に基づく画像には画像へのURLが含まれているため、画像のURLが変更されないと確信している場合は、それをモデルに保存できます。画像自体を保存したい場合は、はい、ペーパークリップやキャリアウェーブなどをお勧めします。

この方法でそれを行いたくない場合は、別の方法であり、私の意見では、JSONをボタン付きで、上記のことを実行するコントローラーアクションにポストバックすることです。

編集:たとえば、ApiCallと呼ばれるモデルを保存するには、コントローラーアクションがヒットするたびに新しいdbエントリを作成できます。コントローラアクションが呼び出されget_info、記述したJSON応答を構築できるようにするパラメータが渡されたとします。あなたは以下のようなことをすることができます。

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

あなたは明らかにあなたが持っていたすべての/すべての情報を含めることができます。または、応答全体をYAML文字列として1つのdb属性に格納し、情報を取得するときに情報を解析することもできます。

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