質問編集履歴

3

情報の修正を行いました。

2021/06/11 10:28

投稿

n-s0120
n-s0120

スコア1

test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,12 @@
28
28
 
29
29
 
30
30
 
31
+ 画像3(修正後)
32
+
33
+ ![イメージ説明](e19aa32ea3d23622e7fb9bacdd0ed109.png)
34
+
35
+
36
+
31
37
  ### 発生している問題・エラーメッセージ
32
38
 
33
39
  5段階評価で表示したいが、一瞬だけ、星が10個表示されてしまう。
@@ -130,43 +136,7 @@
130
136
 
131
137
  <% books.each do |book| %>
132
138
 
133
- <tr>
134
-
135
- <td>
136
-
137
- <%= link_to user_path(book.user) do %>
138
-
139
- <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-icon.jpg") %>
140
-
141
- <% end %>
142
-
143
- </td>
144
-
145
- <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td>
146
-
147
- <td><%= book.body %></td>
148
-
149
-
150
-
151
- <td id="favorite-btn-<%= book.id %>">
152
-
153
- <%= render partial: "favorites/favorite-btn",locals: {book: book } %>
139
+ <%= render 'books/book', book: book %>
154
-
155
- </td>
156
-
157
-
158
-
159
- <td id="comments">
160
-
161
- コメント数:<%= book.book_comments.count %>
162
-
163
- </td>
164
-
165
-
166
-
167
- <td class="book-evaluation" data-score="<%= book.evaluation %>"></td>
168
-
169
- </tr>
170
140
 
171
141
  <% end %>
172
142
 
@@ -174,232 +144,276 @@
174
144
 
175
145
  </table>
176
146
 
177
-
147
+ ```
148
+
149
+
150
+
151
+ ```Ruby
152
+
153
+
154
+
155
+ 【books_controller.erb】
156
+
157
+
158
+
159
+ class BooksController < ApplicationController
160
+
161
+
162
+
163
+ def show
164
+
165
+ @book = Book.find(params[:id])
166
+
167
+ @book_new = Book.new
168
+
169
+ @user = @book.user
170
+
171
+ @book_comment = BookComment.new
172
+
173
+ end
174
+
175
+
176
+
177
+ def index
178
+
179
+ @books = Book.all
180
+
181
+ @book = Book.new
182
+
183
+ end
184
+
185
+
186
+
187
+ def create
188
+
189
+ @book = Book.new(book_params)
190
+
191
+ @book.user_id = current_user.id
192
+
193
+ if @book.save
194
+
195
+ redirect_to book_path(@book), notice: "You have created book successfully."
196
+
197
+ else
198
+
199
+ @books = Book.all
200
+
201
+ render 'index'
202
+
203
+ end
204
+
205
+ end
206
+
207
+
208
+
209
+ def edit
210
+
211
+ @book = Book.find(params[:id])
212
+
213
+ @user = @book.user
214
+
215
+ if @user == current_user
216
+
217
+ render :edit
218
+
219
+ else
220
+
221
+ redirect_to books_path
222
+
223
+ end
224
+
225
+ end
226
+
227
+
228
+
229
+ def update
230
+
231
+ @book = Book.find(params[:id])
232
+
233
+ if @book.update(book_params)
234
+
235
+ redirect_to book_path(@book), notice: "You have updated book successfully."
236
+
237
+ else
238
+
239
+ render "edit"
240
+
241
+ end
242
+
243
+ end
244
+
245
+
246
+
247
+ def destroy
248
+
249
+ @book = Book.find(params[:id])
250
+
251
+ @book.delete
252
+
253
+ redirect_to books_path
254
+
255
+ end
256
+
257
+
258
+
259
+ def search
260
+
261
+ @range = params[:range]
262
+
263
+ search = params[:search]
264
+
265
+ keyword = params[:keyword]
266
+
267
+ @books = User.search(search, keyword)
268
+
269
+ end
270
+
271
+
272
+
273
+ private
274
+
275
+
276
+
277
+ def book_params
278
+
279
+ params.require(:book).permit(:title, :body, :evaluation)
280
+
281
+ end
282
+
283
+
284
+
285
+ end
286
+
287
+
288
+
289
+ ```
290
+
291
+
292
+
293
+ ```Ruby
294
+
295
+ 【book.rb】
296
+
297
+ class Book < ApplicationRecord
298
+
299
+ belongs_to :user
300
+
301
+ has_many :book_comments, dependent: :destroy
302
+
303
+ has_many :favorites, dependent: :destroy
304
+
305
+
306
+
307
+ def favorited_by?(user)
308
+
309
+ favorites.where(user_id: user.id).exists?
310
+
311
+ end
312
+
313
+
314
+
315
+ validates :title, presence: true
316
+
317
+ validates :body, presence: true, length: {maximum: 200}
318
+
319
+ validates :evaluation, numericality: {
320
+
321
+ less_than_or_equal_to: 5,
322
+
323
+ greater_than_or_equal_to: 1
324
+
325
+ }
326
+
327
+
328
+
329
+ def self.search(searches, keywords)
330
+
331
+ if searches == "perfect_match"
332
+
333
+ @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}", "#{keywords}")
334
+
335
+ elsif searches == "forward_match"
336
+
337
+ @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}%", "#{keywords}%")
338
+
339
+ elsif searches == "backward_match"
340
+
341
+ @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}", "%#{keywords}")
342
+
343
+ else
344
+
345
+ @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}%", "%#{keywords}%")
346
+
347
+ end
348
+
349
+ end
350
+
351
+ end
352
+
353
+
354
+
355
+ ```
356
+
357
+
358
+
359
+ ```Ruby
360
+
361
+
362
+
363
+ 【books/_book.html.erb】
364
+
365
+ <tr>
366
+
367
+ <td>
368
+
369
+ <%= link_to user_path(book.user) do %>
370
+
371
+ <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-icon.jpg") %>
372
+
373
+ <% end %>
374
+
375
+ </td>
376
+
377
+ <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td>
378
+
379
+ <td><%= book.body %></td>
380
+
381
+
382
+
383
+ <td id="favorite-btn-<%= book.id %>">
384
+
385
+ <%= render partial: "favorites/favorite-btn",locals: {book: book } %>
386
+
387
+ </td>
388
+
389
+
390
+
391
+ <td id="comments">
392
+
393
+ コメント数:<%= book.book_comments.count %>
394
+
395
+ </td>
396
+
397
+
398
+
399
+ <td class="book-evaluation" data-score="<%= book.evaluation %>"></td>
400
+
401
+ </tr>
402
+
403
+
178
404
 
179
405
  <script>
180
406
 
181
- $('.book-evaluation').raty({
407
+ $('.book-evaluation').raty({
182
-
408
+
183
- readOnly: true,
409
+ readOnly: true,
184
-
185
- score: function() {
410
+
186
-
187
- return $(this).attr('data-score');
188
-
189
- },
190
-
191
- path: '/assets/'
411
+ path: '/assets/'
192
-
412
+
193
- });
413
+ });
194
414
 
195
415
  </script>
196
416
 
197
- ```
198
-
199
-
200
-
201
- ```Ruby
202
-
203
-
204
-
205
- 【books_controller.erb】
206
-
207
-
208
-
209
- class BooksController < ApplicationController
210
-
211
-
212
-
213
- def show
214
-
215
- @book = Book.find(params[:id])
216
-
217
- @book_new = Book.new
218
-
219
- @user = @book.user
220
-
221
- @book_comment = BookComment.new
222
-
223
- end
224
-
225
-
226
-
227
- def index
228
-
229
- @books = Book.all
230
-
231
- @book = Book.new
232
-
233
- end
234
-
235
-
236
-
237
- def create
238
-
239
- @book = Book.new(book_params)
240
-
241
- @book.user_id = current_user.id
242
-
243
- if @book.save
244
-
245
- redirect_to book_path(@book), notice: "You have created book successfully."
246
-
247
- else
248
-
249
- @books = Book.all
250
-
251
- render 'index'
252
-
253
- end
254
-
255
- end
256
-
257
-
258
-
259
- def edit
260
-
261
- @book = Book.find(params[:id])
262
-
263
- @user = @book.user
264
-
265
- if @user == current_user
266
-
267
- render :edit
268
-
269
- else
270
-
271
- redirect_to books_path
272
-
273
- end
274
-
275
- end
276
-
277
-
278
-
279
- def update
280
-
281
- @book = Book.find(params[:id])
282
-
283
- if @book.update(book_params)
284
-
285
- redirect_to book_path(@book), notice: "You have updated book successfully."
286
-
287
- else
288
-
289
- render "edit"
290
-
291
- end
292
-
293
- end
294
-
295
-
296
-
297
- def destroy
298
-
299
- @book = Book.find(params[:id])
300
-
301
- @book.delete
302
-
303
- redirect_to books_path
304
-
305
- end
306
-
307
-
308
-
309
- def search
310
-
311
- @range = params[:range]
312
-
313
- search = params[:search]
314
-
315
- keyword = params[:keyword]
316
-
317
- @books = User.search(search, keyword)
318
-
319
- end
320
-
321
-
322
-
323
- private
324
-
325
-
326
-
327
- def book_params
328
-
329
- params.require(:book).permit(:title, :body, :evaluation)
330
-
331
- end
332
-
333
-
334
-
335
- end
336
-
337
-
338
-
339
- ```
340
-
341
-
342
-
343
- ```Ruby
344
-
345
- 【book.rb】
346
-
347
- class Book < ApplicationRecord
348
-
349
- belongs_to :user
350
-
351
- has_many :book_comments, dependent: :destroy
352
-
353
- has_many :favorites, dependent: :destroy
354
-
355
-
356
-
357
- def favorited_by?(user)
358
-
359
- favorites.where(user_id: user.id).exists?
360
-
361
- end
362
-
363
-
364
-
365
- validates :title, presence: true
366
-
367
- validates :body, presence: true, length: {maximum: 200}
368
-
369
- validates :evaluation, numericality: {
370
-
371
- less_than_or_equal_to: 5,
372
-
373
- greater_than_or_equal_to: 1
374
-
375
- }
376
-
377
-
378
-
379
- def self.search(searches, keywords)
380
-
381
- if searches == "perfect_match"
382
-
383
- @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}", "#{keywords}")
384
-
385
- elsif searches == "forward_match"
386
-
387
- @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}%", "#{keywords}%")
388
-
389
- elsif searches == "backward_match"
390
-
391
- @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}", "%#{keywords}")
392
-
393
- else
394
-
395
- @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}%", "%#{keywords}%")
396
-
397
- end
398
-
399
- end
400
-
401
- end
402
-
403
417
 
404
418
 
405
419
  ```

2

情報の追加を行いました。

2021/06/11 10:28

投稿

n-s0120
n-s0120

スコア1

test CHANGED
File without changes
test CHANGED
@@ -198,6 +198,214 @@
198
198
 
199
199
 
200
200
 
201
+ ```Ruby
202
+
203
+
204
+
205
+ 【books_controller.erb】
206
+
207
+
208
+
209
+ class BooksController < ApplicationController
210
+
211
+
212
+
213
+ def show
214
+
215
+ @book = Book.find(params[:id])
216
+
217
+ @book_new = Book.new
218
+
219
+ @user = @book.user
220
+
221
+ @book_comment = BookComment.new
222
+
223
+ end
224
+
225
+
226
+
227
+ def index
228
+
229
+ @books = Book.all
230
+
231
+ @book = Book.new
232
+
233
+ end
234
+
235
+
236
+
237
+ def create
238
+
239
+ @book = Book.new(book_params)
240
+
241
+ @book.user_id = current_user.id
242
+
243
+ if @book.save
244
+
245
+ redirect_to book_path(@book), notice: "You have created book successfully."
246
+
247
+ else
248
+
249
+ @books = Book.all
250
+
251
+ render 'index'
252
+
253
+ end
254
+
255
+ end
256
+
257
+
258
+
259
+ def edit
260
+
261
+ @book = Book.find(params[:id])
262
+
263
+ @user = @book.user
264
+
265
+ if @user == current_user
266
+
267
+ render :edit
268
+
269
+ else
270
+
271
+ redirect_to books_path
272
+
273
+ end
274
+
275
+ end
276
+
277
+
278
+
279
+ def update
280
+
281
+ @book = Book.find(params[:id])
282
+
283
+ if @book.update(book_params)
284
+
285
+ redirect_to book_path(@book), notice: "You have updated book successfully."
286
+
287
+ else
288
+
289
+ render "edit"
290
+
291
+ end
292
+
293
+ end
294
+
295
+
296
+
297
+ def destroy
298
+
299
+ @book = Book.find(params[:id])
300
+
301
+ @book.delete
302
+
303
+ redirect_to books_path
304
+
305
+ end
306
+
307
+
308
+
309
+ def search
310
+
311
+ @range = params[:range]
312
+
313
+ search = params[:search]
314
+
315
+ keyword = params[:keyword]
316
+
317
+ @books = User.search(search, keyword)
318
+
319
+ end
320
+
321
+
322
+
323
+ private
324
+
325
+
326
+
327
+ def book_params
328
+
329
+ params.require(:book).permit(:title, :body, :evaluation)
330
+
331
+ end
332
+
333
+
334
+
335
+ end
336
+
337
+
338
+
339
+ ```
340
+
341
+
342
+
343
+ ```Ruby
344
+
345
+ 【book.rb】
346
+
347
+ class Book < ApplicationRecord
348
+
349
+ belongs_to :user
350
+
351
+ has_many :book_comments, dependent: :destroy
352
+
353
+ has_many :favorites, dependent: :destroy
354
+
355
+
356
+
357
+ def favorited_by?(user)
358
+
359
+ favorites.where(user_id: user.id).exists?
360
+
361
+ end
362
+
363
+
364
+
365
+ validates :title, presence: true
366
+
367
+ validates :body, presence: true, length: {maximum: 200}
368
+
369
+ validates :evaluation, numericality: {
370
+
371
+ less_than_or_equal_to: 5,
372
+
373
+ greater_than_or_equal_to: 1
374
+
375
+ }
376
+
377
+
378
+
379
+ def self.search(searches, keywords)
380
+
381
+ if searches == "perfect_match"
382
+
383
+ @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}", "#{keywords}")
384
+
385
+ elsif searches == "forward_match"
386
+
387
+ @book = Book.where("title LIKE? OR body LIKE?", "#{keywords}%", "#{keywords}%")
388
+
389
+ elsif searches == "backward_match"
390
+
391
+ @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}", "%#{keywords}")
392
+
393
+ else
394
+
395
+ @book = Book.where("title LIKE? OR body LIKE?", "%#{keywords}%", "%#{keywords}%")
396
+
397
+ end
398
+
399
+ end
400
+
401
+ end
402
+
403
+
404
+
405
+ ```
406
+
407
+
408
+
201
409
  ### 試したこと
202
410
 
203
411
 

1

javascriptの問題である可能性があるため、javascriptを追加しました。

2021/06/09 14:33

投稿

n-s0120
n-s0120

スコア1

test CHANGED
File without changes
test CHANGED
File without changes