質問編集履歴
3
言語の追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
2
文の修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,13 +3,8 @@
|
|
3
3
|
実現したいのはfind_videosメソッドをindex.html.erbの:keywordと連動するようにしたいです。
|
4
4
|
■■な機能を実装中に以下のエラーメッセージが発生しました。
|
5
5
|
### 発生している問題・エラーメッセージ
|
6
|
-
```
|
7
|
-
エラーメッセージ
|
8
6
|
|
9
|
-
|
10
|
-
### 該当のソースコード
|
11
|
-
|
12
|
-
```
|
7
|
+
```Ruby on rails
|
13
8
|
ソースコード
|
14
9
|
index.html.erb
|
15
10
|
<% if logged_in? %>
|
@@ -18,8 +13,7 @@
|
|
18
13
|
<form class="form-inline">
|
19
14
|
<%= form_with url: '/', method: :get, local: true do |f| %>
|
20
15
|
<div class="col-md-12 px-0 row">
|
21
|
-
<%= f.text_field :
|
16
|
+
<%= f.text_field :search, :value => @keyword %>
|
22
|
-
<%= f.select :views, {'1万以上': 1, '10万以上': 2, '100万以上':3}, { :include_blank => "指定してください▼" }, :required => true %>
|
23
17
|
<%= f.submit'検索', class: 'btn btn-primary' %>
|
24
18
|
</div>
|
25
19
|
<% end %>
|
@@ -36,7 +30,6 @@
|
|
36
30
|
<tbody>
|
37
31
|
<td>
|
38
32
|
<% @results.items.each do |item| %>
|
39
|
-
<% id = item.id %>
|
40
33
|
<% snippet = item.snippet %>
|
41
34
|
<p><%= snippet.title %></p>
|
42
35
|
<p><%= snippet.channel_title %></p>
|
@@ -53,30 +46,30 @@
|
|
53
46
|
toppages_controller
|
54
47
|
class ToppagesController < ApplicationController
|
55
48
|
before_action :require_user_logged_in, only: [:index]
|
56
|
-
|
49
|
+
require 'google/apis/youtube_v3'
|
57
50
|
def index
|
58
|
-
@results = find_videos(
|
51
|
+
@results = find_videos(@keyword)
|
59
52
|
end
|
60
53
|
|
61
54
|
private
|
62
55
|
GOOGLE_API_KEY =ENV['KEY']
|
63
56
|
|
64
|
-
def find_videos(keyword, after:
|
57
|
+
def find_videos(keyword, after: 7.months.ago, before: Time.now)
|
65
|
-
|
58
|
+
service = Google::Apis::YoutubeV3::YouTubeService.new
|
66
|
-
|
59
|
+
service.key = GOOGLE_API_KEY
|
67
|
-
|
60
|
+
next_page_token = nil
|
68
61
|
opt = {
|
69
62
|
q: keyword,
|
70
63
|
type: 'video',
|
71
|
-
max_results:
|
64
|
+
max_results: 1,
|
72
65
|
order: :date,
|
73
66
|
page_token: next_page_token,
|
74
67
|
published_after: after.iso8601,
|
75
68
|
published_before: before.iso8601
|
76
69
|
}
|
70
|
+
@keyword = opt[:q]
|
77
71
|
service.list_searches(:snippet, opt)
|
78
72
|
end
|
79
|
-
|
80
73
|
end
|
81
74
|
```
|
82
75
|
### 補足情報(FW/ツールのバージョンなど)
|
1
誤字がありましたので修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,21 +1,11 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
Ruby on RailsでYoutubeAPIを用いてキーワード検索や総再生回数を絞り込むフィルター使った検索サイトを作りたいのですが、Youtubeapiと検索機能の紐付けがよくわかりません。
|
3
|
+
実現したいのはfind_videosメソッドをindex.html.erbの:keywordと連動するようにしたいです。
|
3
4
|
■■な機能を実装中に以下のエラーメッセージが発生しました。
|
4
|
-
自分なりに検索機能を実装中に下記のエラーが発生しました。
|
5
5
|
### 発生している問題・エラーメッセージ
|
6
6
|
```
|
7
7
|
エラーメッセージ
|
8
|
-
NoMethodError in Toppages#index
|
9
|
-
Showing /home/ec2-user/environment/Mytube/app/views/toppages/index.html.erb where line #24 raised:
|
10
8
|
|
11
|
-
undefined method `each' for nil:NilClass
|
12
|
-
Extracted source (around line #24):
|
13
|
-
23tbody>
|
14
|
-
24 <% @videos.each do |video| %>
|
15
|
-
25 <tr><td><%= video.snippet.title %></td><td>
|
16
|
-
26 <% end %>
|
17
|
-
27 </tbody>
|
18
|
-
```
|
19
9
|
|
20
10
|
### 該当のソースコード
|
21
11
|
|
@@ -26,9 +16,9 @@
|
|
26
16
|
<p>ログイン完了ユーザ: <%= current_user.name %></p>
|
27
17
|
<div class="container ">
|
28
18
|
<form class="form-inline">
|
29
|
-
<%= form_with
|
19
|
+
<%= form_with url: '/', method: :get, local: true do |f| %>
|
30
20
|
<div class="col-md-12 px-0 row">
|
31
|
-
<%= f.text_field :keyword %>
|
21
|
+
<%= f.text_field :keyword, :value => @results %>
|
32
22
|
<%= f.select :views, {'1万以上': 1, '10万以上': 2, '100万以上':3}, { :include_blank => "指定してください▼" }, :required => true %>
|
33
23
|
<%= f.submit'検索', class: 'btn btn-primary' %>
|
34
24
|
</div>
|
@@ -39,26 +29,57 @@
|
|
39
29
|
<table calss="table">
|
40
30
|
<thead>
|
41
31
|
<tr>
|
42
|
-
<th>
|
32
|
+
<th>Videos</th>
|
43
|
-
<th>Views</th>
|
44
33
|
</tr>
|
45
34
|
</thead>
|
46
35
|
|
47
36
|
<tbody>
|
48
|
-
|
49
|
-
|
50
|
-
|
37
|
+
<td>
|
38
|
+
<% @results.items.each do |item| %>
|
39
|
+
<% id = item.id %>
|
40
|
+
<% snippet = item.snippet %>
|
41
|
+
<p><%= snippet.title %></p>
|
42
|
+
<p><%= snippet.channel_title %></p>
|
43
|
+
<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>
|
44
|
+
<% end %>
|
45
|
+
</td>
|
51
46
|
</tbody>
|
52
47
|
</table>
|
53
48
|
</div>
|
54
49
|
<% else %>
|
55
50
|
<%= render 'sessions/new' %>
|
56
51
|
<% end %>
|
57
|
-
```
|
58
52
|
|
53
|
+
toppages_controller
|
54
|
+
class ToppagesController < ApplicationController
|
55
|
+
before_action :require_user_logged_in, only: [:index]
|
56
|
+
|
59
|
-
|
57
|
+
def index
|
58
|
+
@results = find_videos('')
|
59
|
+
end
|
60
60
|
|
61
|
+
private
|
61
|
-
|
62
|
+
GOOGLE_API_KEY =ENV['KEY']
|
63
|
+
|
64
|
+
def find_videos(keyword, after: 1.months.ago, before: Time.now)
|
62
|
-
|
65
|
+
service = Google::Apis::YoutubeV3::YouTubeService.new
|
66
|
+
service.key = GOOGLE_API_KEY
|
67
|
+
next_page_token = nil
|
68
|
+
opt = {
|
69
|
+
q: keyword,
|
70
|
+
type: 'video',
|
71
|
+
max_results: 2,
|
72
|
+
order: :date,
|
73
|
+
page_token: next_page_token,
|
74
|
+
published_after: after.iso8601,
|
75
|
+
published_before: before.iso8601
|
76
|
+
}
|
77
|
+
service.list_searches(:snippet, opt)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
```
|
63
82
|
### 補足情報(FW/ツールのバージョンなど)
|
64
|
-
使っているツールはAWSです。
|
83
|
+
使っているツールはAWSです。
|
84
|
+
find_videosメソッドはこの方の記事を参考にしました。
|
85
|
+
https://qiita.com/sakakinn/items/46c0d4945e4646f346f6
|