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

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

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

YouTube APIはYouTubeのビデオコンテンツと機能性をウェブサイト、アプリケーション、デバイスに統合することを可能にします。

Ruby on Rails

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

Q&A

0回答

236閲覧

動画検索サイトの検索履歴ページを作りたい

natu494

総合スコア6

YouTube API

YouTube APIはYouTubeのビデオコンテンツと機能性をウェブサイト、アプリケーション、デバイスに統合することを可能にします。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/09/25 12:21

前提・実現したいこと

YoutubeAPIを使ってYotubeアプリを自分で作ってみたいです。

該当のソースコード

発生している問題は動画を検索するキーワードをfind_videos(params[:content])で呼び出しているのですがこれを検索履歴ページに保存したいのですがうまくいきません。

検索ページ <% if logged_in? %> <p>ログイン完了ユーザ: <%= current_user.name %></p> <div class="container "> <form class="form-inline"> <%= form_with(model: @result, local: true, method: :get, class: "search-form" ) do |f| %> <div class="col-md-12 px-0 row"> <%= f.label :content, '検索', value: @result %> <%= f.text_field :content, placeholder: "検索する", class: "search-input" %> <%= f.submit '検索', class: 'btn btn-primary' %> </div> <% end %> </div> <div class="container"> <table calss="table"> <thead> <tr> <th>Videos</th> </tr> </thead> <tbody> <td> <% @data.items.each do |item| %> <% snippet = item.snippet %> <p><%= snippet.title %></p> <p><%= snippet.published_at %><%= snippet.channel_title %></p> <div><iframe width="560" height="315" src="https://www.youtube.com/embed/<%= item.id.video_id %>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div> <% end %> </td> </tbody> </table> </div> <% else %> <%= render 'sessions/new' %> <% end %> 保存ページ <% if logged_in? %> <p>ログイン完了ユーザ: <%= current_user.name %></p> <div class="container"> <table calss="table"> <thead> <tr> <th>検索履歴</th> </tr> </thead> <tbody> <tr> <td> <% @result.each do |result| %> <%= result.content %> <% end %> </td> </tr> </tbody> </table> </div> <% else %> <%= render 'sessions/new' %> <% end %> コントローラー class ToppagesController < ApplicationController before_action :require_user_logged_in, only: [:index] require 'google/apis/youtube_v3' require 'active_support/all' GOOGLE_API_KEY = ENV['KEY'] def index @data = find_videos(params[:content]) end def find_videos(keyword, after: 1.months.ago, before: Time.now) service = Google::Apis::YoutubeV3::YouTubeService.new service.key = GOOGLE_API_KEY next_page_token = nil opt = { q: keyword, type: 'video', max_results: 1, order: :date, page_token: next_page_token, published_after: after.iso8601, published_before: before.iso8601 } service.list_searches(:snippet, opt) end end 保存ページコントローラ class ResultsController < ApplicationController before_action :require_user_logged_in def index @result = Result.all.order(id: :desc).page(params[:page]).per(10) end def new @result = Result.new end def create @result = Result.new(result_params) if @result.save redirect_to @result else render :toppages end end private def result_params params.require(:result).permit(:content) end end

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

ruby '2.6.3'
rails '5.2.2'
AWS Cloud9

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問