2013-01-04 120 views
0

我期待在這裏澄清我的理解或真正理解......我有像這樣節約JSON響應爲對象軌道

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

}

AA JSON響應,我想救它以點擊的link_to後,我的書一定的模型值(我已閱讀的link_to比說button_to更安全)

所以我有一個處理包含在我的控制器解析模塊

module Book::BookFinder 

BOOK_URL = 'https://itunes.apple.com/lookup?isbn=' 

def book_search(search) 
    response = HTTParty.get(BOOK_URL + "#{search}", :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }) 
results = JSON.parse(response.body)["results"] 
end 
    end 

在我的書控制器

class BookController < ApplicationController 
before_filter :authenticate_admin_user! 
include Book::BookFinder 

def searchbook 

end 

def results 
results = book_search(params[:search]) 
@results = results 

end 

def savebook 

Book.create(:author => response["results"]["artistName"]) 

end 
end 

(首先,我看這個,認爲使用Rails寧靜的做法會比較好,所以使用新創建的方法呢?)

我有一個的link_to設置把數據上傳到我的模型

<%= link_to 'Save Book', savebook_path, :method => :post %> 

和我的路線

scope :controller => :book do 
get 'searchbook' 
get 'results' 
post 'savebook' 

end 

所以目前我在點擊link_to的時候收到錯誤信息,說錯誤的參數個數(0代表1)。

,因爲我收到的數據外已拋出我一點點,用新的形式發佈數據打交道時,創建,編輯等它是有道理的,但在這裏,我有點失去

任何人都可以提供一些建議/解決方案,以幫助我瞭解需要採取什麼

感謝

+0

是什麼,而不是

<%= link_to 'Save Book', savebook_path, :method => :post %> 

使用

... <%= form_for @book do |f| %> <%= f.hidden_field :author %> ... <%= f.submit 'Save book' %> <% end %> 

而且絕對標準圖書控制器這條線呢? 'json_response = JSON.parse [「結果」]'?應該解析和存儲的json響應在哪裏? – dimuch

+0

我的印象是這條線分析返回的JSON對象? – Richlewis

+0

根本不是。 'json_response = JSON.parse [「results」]'就像'res = JSON.parse''json_response = res ['results']''。 – dimuch

回答

3

我會建議你改變架構的位。假設你收到結果的行動API響應:

結果來看,與創建行動

+0

感謝您的更新,爲什麼我現在在提交搜索時爲Book:Module提供未定義方法'new' – Richlewis

+0

它是一個命名衝突,因爲我的模塊是Book :: BookFinder,我正在創建一個名爲Book的對象? – Richlewis

+0

好吧,好像一個模塊不能被實例化,並且需要使用一個類。確保從這裏去哪裏 – Richlewis