質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Q&A

1回答

824閲覧

RoutingErrorがでました。GoogleBooksAPIを導入したいです。

kinakoo

総合スコア5

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ルーティング

ルーティングとは、TCP/IPネットワークにおいて、目的のホストまでパケットを送る為のパス選定のプロセスを言います。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

0グッド

0クリップ

投稿2021/05/28 01:53

前提・実現したいこと

GoogleBooksAPIを用いて、本登録ができるように実装しようとしています。
検索結果のところにキーワード検索した結果が表示されるように実装したいと考えています。
イメージ説明

発生している問題・エラーメッセージ

キーワード検索を押すと、routing error が表示されます。
イメージ説明

該当のソースコード

config>routes.rb

rails

1Rails.application.routes.draw do 2 devise_for :users 3 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 4 resources :books do 5 resources :likes, only: [:create, :destroy] 6 root 'books#index' 7 get '/google_book_search', to: 'books#new' 8 end 9 10end

app>controllers>books_contoller.rb

rails

1class BooksController < ApplicationController 2 before_action :authenticate_user!, only: [:new, :create] 3 def index 4 if params[:search] == nil || '' 5 @books= Book.all 6 elsif params[:search] == '' 7 @books= Book.all 8 else 9 @books = Book.where("body LIKE ? ",'%' + params[:search] + '%') 10 end 11 @rank_books = Book.all.sort {|a,b| b.liked_users.count <=> a.liked_users.count} 12 end 13 14 def new 15 @book = Book.new 16 end 17 18 def create 19 book = Book.new(book_params) 20 if book.save 21 redirect_to :action => "index" 22 else 23 redirect_to :action => "new" 24 end 25 end 26 27 def show 28 @book = Book.find(params[:id]) 29 end 30 31 def edit 32 @book = Book.find(params[:id]) 33 end 34 35 def update 36 book = Book.find(params[:id]) 37 if book.update(book_params) 38 redirect_to :action => "show", :id => book.id 39 else 40 redirect_to :action => "new" 41 end 42 end 43 44 def destroy 45 book = Book.find(params[:id]) 46 book.destroy 47 redirect_to action: :index 48 end 49 50 private 51 def book_params 52 params.require(:book).permit(:title, :learn, :about, :category, :overrall) 53 end 54end

lib>google_book_serarch.rb

rails

1require 'net/https' 2 3class GoogleBookSearch 4 include Singleton 5 6 def initialize 7 @uri = URI.parse 'https://www.googleapis.com/books/v1/volumes' # URIにparseする 8 @http = Net::HTTP.new @uri.host, @uri.port # httpインスタンスを生成 9 @http.use_ssl = true # httpsで通信を行うようにする。指定しないとエラーする場合がある 10 end 11 12 def search(keyword) # 検索を行って配列を返すメソッド 13 create_request(keyword) 14 15 @res['items'].map do |item| 16 info = item['volumeInfo'] 17 { 18 id: item['id'], 19 title: info['title'], 20 price: item['saleInfo']['listPrice'].nil? ? ' - ' : item['saleInfo']['listPrice']['amount'].floor.to_s, 21 authors: info['authors'], 22 publisher: info['publisher'], 23 published_date: info['publishedDate'], 24 description: info['description'], 25 image_path: info['imageLinks']['smallThumbnail'] 26 } 27 end 28 end 29 30 private 31 32 def create_request(keyword) # httpリクエストを投げてレスポンスを返り値とするメソッド 33 req = Net::HTTP::Get.new @uri.path + "?q=#{keyword}" # getリクエストを生成 34 _res = @http.request req # httpリクエストを投げる 35 @res = JSON.parse(_res.body) # レスポンスをJSON形式にparseする 36 end 37 38end

app>views>books>new.html.erb

rails

1<h1>???? BOOK SHARE ????</h1> 2<h3>本の新規登録</h3> 3 4 <%= form_tag('/google_book_search', method: :get) do %> 5 <div class="input-group"> 6 <%= search_field_tag "keyword", params[:keyword], class: "form-control", placeholder: "キーワード検索" %> 7 <span class="input-group-btn"> 8 <%= submit_tag "検索", class: "btn btn-primary" %> 9 </span> 10 </div> 11 <% end %> 12<h3>検索結果(↓手入力でもOK〜!)</h3> 13<% if @books.present? %> 14 <% @books.each do |book| %> 15 <img src="<%= book[1] %>" width="40" vspace="2"> 16 <%= book[0] %> | 17 <%= book[2] %> | 18 <%= book[3] %> <br> 19 <% end %> 20<% end %> 21 22<%= form_for @book do |f| %> 23 <div class="field"> 24 <%= f.label :タイトル %> 25 <%= f.text_field :title, :size => 30 %> 26 27 <h3>総合評価</h3> 28 <div class="post_form"> 29 <%= f.radio_button :overrall, 5 ,id: 'star1'%> 30 <label for="star1"><span class="text">感動した</span>★</label> 31 32 <%= f.radio_button :overrall, 4 ,id: 'star2'%> 33 <label for="star2"><span class="text">イイネ!</span>★</label> 34 35 <%= f.radio_button :overrall, 3 ,id: 'star3'%> 36 <label for="star3"><span class="text">まあまあ</span>★</label> 37 38 <%= f.radio_button :overrall, 2 ,id: 'star4'%> 39 <label for="star4"><span class="text">うーん。</span>★</label> 40 41 <%= f.radio_button :overrall, 1 ,id: 'star5'%> 42 <label for="star5"><span class="text">最悪</span>★</label> 43 </div> 44 45 <div class="field"> 46 <%= f.label :本のカテゴリー %> 47 <%= f.select :category, 48 [["自己啓発", "自己啓発"], 49 ["小説", "小説"], 50 ["学習参考書", "学習参考書"], 51 ["その他", "その他"]], include_blank: "選択して下さい" %> 52 </div> 53 <br> 54 55 <div class="field"> 56 <%= f.label :どんな本?(一言で!) %> 57 <%= f.text_field :learn,size: 140 %> 58 </div> 59 <br> 60 61 <div class="field"> 62 <%= f.label :この本から学んだこと %> 63 <%= f.text_field :about,size: 140 %> 64 </div> 65 <br> 66 67 <%= f.submit "本を投稿する" %> 68<% end %> 69<br> 70<br> 71 72 73<%= link_to "本の一覧に戻る", books_path %>

試したこと

routingが間違いだということで、ルーティングをみてみましたが、
ルーティングの書き方を今ひとつ理解していなくて、わからなくなってしまいました。

初学者すぎて、質問するのも申し訳ないのですが、教えて頂けると嬉しいです。

補足情報(FW/ツールのバージョンなど)

ruby 2.7.2p137
Rails 6.1.3.2
psql (PostgreSQL) 13.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

resources :books do
get '/google_book_search', to: 'books#new'
end

これは、/books/:id/google_book_searchが定義される筈です。
また、to: books#newも不思議です。

ruby

1resources :books do 2# 略 3end 4get '/google_book_search', to: 'books#google_search'

とし

ruby

1class BooksController 2#略 3 def google_search 4 @res = GoogleBookSearch.instance.search(params[:keyword]) 5 end 6end

みたいな感じですかね
おそらく、formタグをremote: trueにしてjs返した方が便利かとは思います。

投稿2021/05/28 08:35

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問