前提・実現したいこと
タイトル通りのアプリを作成しております。
エラーによれば、nilになっているとの事なので、そもそもデータの取得ができていないように思います。
また、ビューにどのように表示すればいいのかもわからないので、教えて頂きたく存じます。
発生している問題・エラーメッセージ
NoMethodError in Youtube#index undefined method `each' for nil:NilClass Extracted source (around line #1): <% (@videos).each do |videoId| %> <iframe src="https://www.youtube.com/embed/<%= videoId %>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe> <% end %>
該当のソースコード
Gemfile
gem 'google-api-client', '> 0.11'> 2.1'
gem 'trollop', '
youtube_controller.rb
class YoutubeController < ApplicationController
require 'rubygems'
require 'google/api_client'
require 'trollop'
DEVELOPER_KEY = '自分のキーを入力しています'
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'
def get_service
client = Google::APIClient.new(
:key => DEVELOPER_KEY,
:authorization => nil,
:application_name => $PROGRAM_NAME,
:application_version => '1.0.0'
)
youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
return client, youtube
end
def get_data(keyword)
opts = Trollop::options do
opt :q, 'Search term', :type => String, :default => keyword
opt :max_results, 'Max results', :type => :int, :default => 50
end
client, youtube = get_service begin search_response = client.execute!( :api_method => youtube.search.list, :parameters => { :part => 'snippet', :q => opts[:q], :maxResults => opts[:max_results], :order => opts[:order], :regionCode => opts[:regionCode] } ) @videos = [] search_response.data.items.each do |search_result| @videos << "#{search_result.snippet.title} (#{search_result.id.videoId})" puts e.result.body end end
end
end
index.html.erb
<% (@videos).each do |videoId| %>
<iframe src="https://www.youtube.com/embed/<%= videoId %>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe> <% end %>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。