繁体   English   中英

将JSON响应保存为对象

[英]saving JSON response as an object rails

我想在这里澄清我的理解或实际上理解...我有一个类似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}]

}

并且我想在单击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宁静的实践会更好,所以使用new和create方法?

我有一个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)。

因为我从外部获取数据,这使我感到有些不适,当使用新的,创建,编辑等方式处理来自表单的数据时,这很有意义,但是在这里我有点迷失了

谁能提供一些建议/解决方案来帮助我了解需要发生的事情

谢谢

我建议您稍微改变一下架构。 假设您在结果操作中收到api响应:

def results
  ...
  @results = book_search(params[:search])
  @book = Book.new
  @book.author = results[0]["artistName"]
  ...
end

def create
  @book = Book.new(params[:book])
  if @book.save
    redirect_to @book, notice: 'Book was successfully saved'
  else
    render action:new
  end
end

结果视图,而不是

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

采用

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

和绝对标准的Book控制器具​​有create action

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM