質問編集履歴

3

書式の修正

2021/07/02 04:06

投稿

kinakoo
kinakoo

スコア5

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  本のタイトルと著者名は検索から取得することができるのですが、画像の取得の方法がわかりません。
8
8
 
9
- ![イメージ説明](6407a8814322d6bde63b2a02b990a3c5.png)
9
+
10
10
 
11
11
 
12
12
 
@@ -172,32 +172,14 @@
172
172
 
173
173
  <%= form.hidden_field :img, class: 'form-control', value: "#{@img}" %>
174
174
 
175
- <br>
175
+
176
-
177
- <br>
176
+
178
-
179
- <div class="field">
180
-
181
- <%= form.label :本の画像 %>
182
-
183
- <%= form.file_field :image %>
184
-
185
- </div>
177
+ <% end %>
178
+
179
+ <%= link_to "本の一覧に戻る", books_path %>
186
180
 
187
181
 
188
182
 
189
- <%= form.submit "本を投稿する" %>
190
-
191
- <br>
192
-
193
- <br>
194
-
195
- <% end %>
196
-
197
- <%= link_to "本の一覧に戻る", books_path %>
198
-
199
-
200
-
201
183
  ```
202
184
 
203
185
 

2

書式の改善

2021/07/02 04:06

投稿

kinakoo
kinakoo

スコア5

test CHANGED
File without changes
test CHANGED
@@ -206,7 +206,7 @@
206
206
 
207
207
 
208
208
 
209
- このサイト([GoogleBooksAPIとRailsでお手軽に書籍登録](https://qiita.com/hukurouo/items/90727e996572f03fc79a))を参考にGoogleBooksAPIを実装しました。
209
+ このサイト([GoogleBooksAPIとRailsでお手軽に書籍登録](https://qiita.com/hukurouo/items/90727e996572f03fc79a))を参考にGoogleBooksAPIを実装しました。公式ドキュメントに書かれていたimaagelinksが使えるのではないかと試行錯誤中です。
210
210
 
211
211
 
212
212
 

1

書式の改善

2021/07/01 05:20

投稿

kinakoo
kinakoo

スコア5

test CHANGED
File without changes
test CHANGED
@@ -26,208 +26,76 @@
26
26
 
27
27
  before_action :authenticate_user!, only: [:new, :create]
28
28
 
29
+
30
+
31
+
32
+
29
- def index
33
+ def new
34
+
30
-
35
+ @book = Book.new
36
+
37
+
38
+
31
- if params[:search] == nil
39
+ if params[:keyword].present?
32
-
33
- @books= Book.all.page(params[:page]).per(5)
40
+
34
-
35
- elsif params[:search] == ""
41
+ require 'net/http'
36
-
42
+
37
- @books= Book.all.page(params[:page]).per(5)
43
+ url = 'https://www.googleapis.com/books/v1/volumes?q='
38
-
39
- else
44
+
40
-
41
- @search = params[:search]
45
+ request = url + params[:keyword]
46
+
42
-
47
+ enc_str = URI.encode(request)
48
+
49
+ uri = URI.parse(enc_str)
50
+
51
+ json = Net::HTTP.get(uri)
52
+
53
+ @bookjson = JSON.parse(json)
54
+
55
+
56
+
57
+ count = 5 #検索結果に表示する数
58
+
59
+ @books = Array.new(count).map{Array.new(4)}
60
+
61
+ count.times do |x|
62
+
63
+ @books[x][0] = @bookjson.dig("items", x, "volumeInfo", "title")
64
+
65
+ @books[x][1] = @bookjson.dig("items", x, "volumeInfo", "imageLinks", "thumbnail")
66
+
67
+ @books[x][2] = @bookjson.dig("items", x, "volumeInfo", "authors")
68
+
69
+ @books[x][2] = @books[x][2].join(',') if @books[x][2] #複数著者をカンマで区切る
70
+
43
- @books = Book.where("title LIKE ? ",'%' + params[:search] + '%').page(params[:page]).per(5)
71
+ @books[x][3] = @bookjson.dig("items", x, "volumeInfo", "industryIdentifiers", 0, "identifier")
72
+
73
+ end
44
74
 
45
75
  end
46
76
 
47
-
77
+ @title = params[:title] if params[:title].present?
48
-
78
+
49
- @rank_books = Book.all.sort {|a,b| b.liked_users.count <=> a.liked_users.count}
79
+ @author = params[:author] if params[:author].present?
80
+
50
-
81
+ @img = params[:image] if params[:image].present?
51
-
52
82
 
53
83
  end
54
84
 
55
85
 
56
86
 
87
+
88
+
89
+
90
+
57
- def new
91
+ private
58
-
92
+
59
- @book = Book.new
93
+ def book_params
60
-
61
-
62
-
63
- if params[:keyword].present?
94
+
64
-
65
- require 'net/http'
66
-
67
- url = 'https://www.googleapis.com/books/v1/volumes?q='
68
-
69
- request = url + params[:keyword]
70
-
71
- enc_str = URI.encode(request)
72
-
73
- uri = URI.parse(enc_str)
74
-
75
- json = Net::HTTP.get(uri)
76
-
77
- @bookjson = JSON.parse(json)
78
-
79
-
80
-
81
- count = 5 #検索結果に表示する数
82
-
83
- @books = Array.new(count).map{Array.new(4)}
84
-
85
- count.times do |x|
86
-
87
- @books[x][0] = @bookjson.dig("items", x, "volumeInfo", "title")
95
+ params.require(:book).permit(:title, :author, :learn, :about, :category, :overrall, :image)
88
-
89
- @books[x][1] = @bookjson.dig("items", x, "volumeInfo", "imageLinks", "thumbnail")
90
-
91
- @books[x][2] = @bookjson.dig("items", x, "volumeInfo", "authors")
92
-
93
- @books[x][2] = @books[x][2].join(',') if @books[x][2] #複数著者をカンマで区切る
94
-
95
- @books[x][3] = @bookjson.dig("items", x, "volumeInfo", "industryIdentifiers", 0, "identifier")
96
-
97
- end
98
-
99
- end
100
-
101
- @title = params[:title] if params[:title].present?
102
-
103
- @author = params[:author] if params[:author].present?
104
-
105
- @img = params[:image] if params[:image].present?
106
96
 
107
97
  end
108
98
 
109
-
110
-
111
-
112
-
113
- def create
114
-
115
- book = Book.new(book_params)
116
-
117
- book.user_id = current_user.id
118
-
119
- if book.save
120
-
121
- redirect_to :action => "index"
122
-
123
- else
124
-
125
- redirect_to :action => "new"
126
-
127
- end
128
-
129
- end
130
-
131
-
132
-
133
- def show
134
-
135
- @book = Book.find(params[:id])
136
-
137
- end
138
-
139
-
140
-
141
- def edit
142
-
143
- @book = Book.find(params[:id])
144
-
145
- if params[:keyword].present?
146
-
147
- require 'net/http'
148
-
149
- url = 'https://www.googleapis.com/books/v1/volumes?q='
150
-
151
- request = url + params[:keyword]
152
-
153
- enc_str = URI.encode(request)
154
-
155
- uri = URI.parse(enc_str)
156
-
157
- json = Net::HTTP.get(uri)
158
-
159
- @bookjson = JSON.parse(json)
160
-
161
-
162
-
163
- count = 5 #検索結果に表示する数
164
-
165
- @books = Array.new(count).map{Array.new(4)}
166
-
167
- count.times do |x|
168
-
169
- @books[x][0] = @bookjson.dig("items", x, "volumeInfo", "title")
170
-
171
- @books[x][1] = @bookjson.dig("items", x, "volumeInfo", "imageLinks", "thumbnail")
172
-
173
- @books[x][2] = @bookjson.dig("items", x, "volumeInfo", "authors")
174
-
175
- @books[x][2] = @books[x][2].join(',') if @books[x][2] #複数著者をカンマで区切る
176
-
177
- @books[x][3] = @bookjson.dig("items", x, "volumeInfo", "industryIdentifiers", 0, "identifier")
178
-
179
- end
180
-
181
- end
182
-
183
- @title = params[:title] if params[:title].present?
184
-
185
- @author = params[:author] if params[:author].present?
186
-
187
- @img = params[:image] if params[:image].present?
188
-
189
- end
190
-
191
-
192
-
193
- def update
194
-
195
- book = Book.find(params[:id])
196
-
197
- if book.update(book_params)
198
-
199
- redirect_to :action => "show", :id => book.id
200
-
201
- else
202
-
203
- redirect_to :action => "new"
204
-
205
- end
206
-
207
- end
208
-
209
-
210
-
211
- def destroy
212
-
213
- book = Book.find(params[:id])
214
-
215
- book.destroy
216
-
217
- redirect_to action: :index
218
-
219
- end
220
-
221
-
222
-
223
- private
224
-
225
- def book_params
226
-
227
- params.require(:book).permit(:title, :author, :learn, :about, :category, :overrall, :image)
228
-
229
- end
230
-
231
99
 
232
100
 
233
101
  end