map関数で変換
- 取得した値を、プロパティに詰め込む。
- jsonのパース用ライブラリも読み込む
require "net/http"
require "open-uri"
require 'uri'
require 'json'
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
//ここがポイント
@books = GoogleBooks.search(params[:search],{:count => 20}).map{|googlebook| book = Book.new
book.title = googlebook.title
book.author = googlebook.authors
book.publish = googlebook.publisher
book.published = googlebook.published_date
book.image_link = googlebook.image_link
book
}
end
private
def set_book
@book = Book.find(params[:id])
end
def book_params
params.require(:book).permit(:title, :author, :published, :image_link)
end
end