这是我第一次尝试从 API 取回数据,并将数据输出到视图。
我想将 ISBN 号放入搜索表单中,并使用http://isbndb.com/获取该特定书籍的数据。这是我到目前为止所拥有的:
控制器:
require 'open-uri'
class BookController < ApplicationController
def searchbook
resp = open("http://isbndb.com/api/books.xml?access_key=#{'API KEY HERE'}&results=texts&index1=isbn&value1=#{params[:isbn]}")
doc = Nokogiri.XML(resp.read)
# ... process response here
end
end
形式:
<%= form_tag({:controller => 'book', :action => 'searchbook'}, {:method => 'get'}) do |select| %>
<%= label_tag :isbn, "Enter ISBN Number" %>
<%= text_field_tag :isbn, params[:isbn] %>
<%= submit_tag "Search" %>
<% end %>
要返回的 XML
<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2005-07-29T03:02:22">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="paul_laurence_dunbar" isbn="0766013502">
<Title>Paul Laurence Dunbar</Title>
<TitleLong>Paul Laurence Dunbar: portrait of a poet</TitleLong>
<AuthorsText>Catherine Reef</AuthorsText>
<PublisherText publisher_id="enslow_publishers">Berkeley Heights, NJ: Enslow Publishers, c2000.</PublisherText>
<Summary>A biography of the poet who faced racism and devoted himself to depicting the black experience in America.</Summary>
<Notes>"Works by Paul Laurence Dunbar": p. 113-114. Includes bibliographical references (p. 124) and index.</Notes>
<UrlsText></UrlsText>
<AwardsText></AwardsText>
</BookData>
</BookList>
</ISBNdb>
如何处理 XML 请求或者我可以阅读什么来了解如何处理?
在哪里可以查看控制台中返回的数据(如果有)?我什至不确定这是否正在做任何事情,但是在我的表单中单击“搜索”后,我被带到了 searchbook 操作,该操作目前是一个空白页面。
我可能离整个答案还有很长的路要走,但这是我第一次这样做。