質問編集履歴

2

見やすいように修正しました。至らず申し訳ない。

2021/11/10 01:00

投稿

sattaku1538
sattaku1538

スコア7

test CHANGED
File without changes
test CHANGED
@@ -22,11 +22,13 @@
22
22
 
23
23
  ※追記
24
24
 
25
-
26
-
27
- 問題がありそうな部分のコードを添付します。
25
+ ```
28
-
26
+
29
- <Schema>
27
+ (schema.rb)
28
+
29
+
30
+
31
+
30
32
 
31
33
  ActiveRecord::Schema.define(version: 2021_11_05_042913) do
32
34
 
@@ -126,7 +128,117 @@
126
128
 
127
129
 
128
130
 
131
+ ```
132
+
133
+
134
+
135
+ ```
136
+
137
+ (users_controller.rb)
138
+
139
+
140
+
141
+ class UsersController < ApplicationController
142
+
143
+ before_action :authenticate_user!
144
+
145
+ before_action :ensure_correct_user, only: [:edit, :update]
146
+
147
+
148
+
149
+ def show
150
+
151
+ @user = User.find(params[:id])
152
+
153
+ @books = @user.books
154
+
155
+ @book = Book.new
156
+
157
+ @today_book = @books.created_today
158
+
159
+ @yesterday_book = @books.created_yesterday
160
+
161
+ @this_week_book = @books.created_this_week
162
+
163
+ @last_week_book = @books.created_last_week
164
+
165
+ end
166
+
167
+
168
+
169
+ def index
170
+
171
+ @users = User.all
172
+
173
+ @book = Book.new
174
+
175
+ end
176
+
177
+
178
+
179
+ def edit
180
+
181
+ @user = User.find(params[:id])
182
+
183
+ end
184
+
185
+
186
+
187
+ def update
188
+
189
+ if @user.update(user_params)
190
+
191
+ redirect_to user_path(@user), notice: "You have updated user successfully."
192
+
193
+ else
194
+
195
+ render "edit"
196
+
197
+ end
198
+
199
+ end
200
+
201
+
202
+
203
+ private
204
+
205
+
206
+
207
+ def user_params
208
+
209
+ params.require(:user).permit(:name, :introduction, :profile_image)
210
+
211
+ end
212
+
213
+
214
+
215
+ def ensure_correct_user
216
+
217
+ @user = User.find(params[:id])
218
+
219
+ unless @user == current_user
220
+
221
+ redirect_to user_path(current_user)
222
+
223
+ end
224
+
225
+ end
226
+
227
+ end
228
+
229
+
230
+
231
+ ```
232
+
233
+
234
+
235
+
236
+
237
+ ```
238
+
129
- <book.rb>
239
+ (books.rb)
240
+
241
+
130
242
 
131
243
  class Book < ApplicationRecord
132
244
 
@@ -198,101 +310,17 @@
198
310
 
199
311
 
200
312
 
201
- <user_controller>
202
-
203
- class UsersController < ApplicationController
204
-
205
- before_action :authenticate_user!
206
-
207
- before_action :ensure_correct_user, only: [:edit, :update]
208
-
209
-
210
-
211
- def show
212
-
213
- @user = User.find(params[:id])
214
-
215
- @books = @user.books
216
-
217
- @book = Book.new
218
-
219
- @today_book = @books.created_today
220
-
221
- @yesterday_book = @books.created_yesterday
222
-
223
- @this_week_book = @books.created_this_week
224
-
225
- @last_week_book = @books.created_last_week
226
-
227
- end
313
+ ```
228
-
229
-
230
-
231
- def index
314
+
232
-
233
- @users = User.all
315
+
234
-
235
- @book = Book.new
316
+
236
-
237
- end
317
+ ```
238
-
239
-
240
-
241
- def edit
318
+
242
-
243
- @user = User.find(params[:id])
319
+
244
-
245
- end
320
+
246
-
247
-
248
-
249
- def update
250
-
251
- if @user.update(user_params)
252
-
253
- redirect_to user_path(@user), notice: "You have updated user successfully."
254
-
255
- else
256
-
257
- render "edit"
258
-
259
- end
260
-
261
- end
262
-
263
-
264
-
265
- private
266
-
267
-
268
-
269
- def user_params
270
-
271
- params.require(:user).permit(:name, :introduction, :profile_image)
272
-
273
- end
274
-
275
-
276
-
277
- def ensure_correct_user
278
-
279
- @user = User.find(params[:id])
280
-
281
- unless @user == current_user
282
-
283
- redirect_to user_path(current_user)
284
-
285
- end
286
-
287
- end
288
-
289
- end
290
-
291
-
292
-
293
-
294
-
295
- <部分テンプレート _book_index.html.erb>
321
+ (_book_index.html.erb)
322
+
323
+
296
324
 
297
325
 
298
326
 
@@ -446,9 +474,15 @@
446
474
 
447
475
  </div>
448
476
 
449
-
477
+ ```
478
+
479
+
480
+
450
-
481
+ ```
482
+
451
- <user/show>
483
+ (show.html.erb)
484
+
485
+
452
486
 
453
487
  <div class='container px-5 px-sm-0'>
454
488
 
@@ -481,3 +515,5 @@
481
515
  </div>
482
516
 
483
517
  </div>
518
+
519
+ ```

1

コードを加筆しました。

2021/11/10 01:00

投稿

sattaku1538
sattaku1538

スコア7

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,467 @@
17
17
 
18
18
 
19
19
  原因を教えてください。
20
+
21
+
22
+
23
+ ※追記
24
+
25
+
26
+
27
+ 問題がありそうな部分のコードを添付します。
28
+
29
+ <Schema>
30
+
31
+ ActiveRecord::Schema.define(version: 2021_11_05_042913) do
32
+
33
+
34
+
35
+ create_table "book_comments", force: :cascade do |t|
36
+
37
+ t.integer "user_id"
38
+
39
+ t.integer "book_id"
40
+
41
+ t.text "comment"
42
+
43
+ t.datetime "created_at", null: false
44
+
45
+ t.datetime "updated_at", null: false
46
+
47
+ end
48
+
49
+
50
+
51
+ create_table "books", force: :cascade do |t|
52
+
53
+ t.string "title"
54
+
55
+ t.text "body"
56
+
57
+ t.integer "user_id"
58
+
59
+ t.datetime "created_at", null: false
60
+
61
+ t.datetime "updated_at", null: false
62
+
63
+ end
64
+
65
+
66
+
67
+ create_table "favorites", force: :cascade do |t|
68
+
69
+ t.integer "user_id"
70
+
71
+ t.integer "book_id"
72
+
73
+ t.datetime "created_at", null: false
74
+
75
+ t.datetime "updated_at", null: false
76
+
77
+ end
78
+
79
+
80
+
81
+ create_table "relationships", force: :cascade do |t|
82
+
83
+ t.integer "follower_id"
84
+
85
+ t.integer "followed_id"
86
+
87
+ t.datetime "created_at", null: false
88
+
89
+ t.datetime "updated_at", null: false
90
+
91
+ end
92
+
93
+
94
+
95
+ create_table "users", force: :cascade do |t|
96
+
97
+ t.string "email", default: "", null: false
98
+
99
+ t.string "encrypted_password", default: "", null: false
100
+
101
+ t.string "reset_password_token"
102
+
103
+ t.datetime "reset_password_sent_at"
104
+
105
+ t.datetime "remember_created_at"
106
+
107
+ t.string "name"
108
+
109
+ t.text "introduction"
110
+
111
+ t.string "profile_image_id"
112
+
113
+ t.datetime "created_at", null: false
114
+
115
+ t.datetime "updated_at", null: false
116
+
117
+ t.index ["email"], name: "index_users_on_email", unique: true
118
+
119
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
120
+
121
+ end
122
+
123
+
124
+
125
+ end
126
+
127
+
128
+
129
+ <book.rb>
130
+
131
+ class Book < ApplicationRecord
132
+
133
+ belongs_to :user
134
+
135
+ has_many :favorites, dependent: :destroy
136
+
137
+ has_many :book_comments, dependent: :destroy
138
+
139
+ has_many :favorited_users, through: :favorites, source: :user #いいね数で順番を変えるための
140
+
141
+
142
+
143
+ validates :title, presence: true
144
+
145
+ validates :body, presence: true, length: { maximum: 200 }
146
+
147
+
148
+
149
+ # いいね機能
150
+
151
+ def favorited_by?(user)
152
+
153
+ favorites.where(user_id: user.id).exists?
154
+
155
+ end
156
+
157
+
158
+
159
+ # 検索機能
160
+
161
+ def self.search_for(content, method)
162
+
163
+ if method == 'perfect'
164
+
165
+ Book.where(title: content)
166
+
167
+ elsif method == 'forward'
168
+
169
+ Book.where('title LIKE ?', content+'%')
170
+
171
+ elsif method == 'backward'
172
+
173
+ Book.where('title LIKE ?', '%'+content)
174
+
175
+ else
176
+
177
+ Book.where('title LIKE ?', '%'+content+'%')
178
+
179
+ end
180
+
181
+ end
182
+
183
+
184
+
185
+ # 投稿日数を表示させる
186
+
187
+ scope :created_today, -> { where(created_at: Time.zone.now.all_day) }
188
+
189
+ scope :created_yesterday, -> { where(created_at: 1.day.ago.all_day) }
190
+
191
+ scope :created_this_week, -> { where(created_at: 6.day.ago.beginning_of_day..Time.zone.now.end_of_day) }
192
+
193
+ scope :created_last_week, -> { where(created_at: 2.week.ago.beginning_of_day..1.week.ago.end_of_day) }
194
+
195
+
196
+
197
+ end
198
+
199
+
200
+
201
+ <user_controller>
202
+
203
+ class UsersController < ApplicationController
204
+
205
+ before_action :authenticate_user!
206
+
207
+ before_action :ensure_correct_user, only: [:edit, :update]
208
+
209
+
210
+
211
+ def show
212
+
213
+ @user = User.find(params[:id])
214
+
215
+ @books = @user.books
216
+
217
+ @book = Book.new
218
+
219
+ @today_book = @books.created_today
220
+
221
+ @yesterday_book = @books.created_yesterday
222
+
223
+ @this_week_book = @books.created_this_week
224
+
225
+ @last_week_book = @books.created_last_week
226
+
227
+ end
228
+
229
+
230
+
231
+ def index
232
+
233
+ @users = User.all
234
+
235
+ @book = Book.new
236
+
237
+ end
238
+
239
+
240
+
241
+ def edit
242
+
243
+ @user = User.find(params[:id])
244
+
245
+ end
246
+
247
+
248
+
249
+ def update
250
+
251
+ if @user.update(user_params)
252
+
253
+ redirect_to user_path(@user), notice: "You have updated user successfully."
254
+
255
+ else
256
+
257
+ render "edit"
258
+
259
+ end
260
+
261
+ end
262
+
263
+
264
+
265
+ private
266
+
267
+
268
+
269
+ def user_params
270
+
271
+ params.require(:user).permit(:name, :introduction, :profile_image)
272
+
273
+ end
274
+
275
+
276
+
277
+ def ensure_correct_user
278
+
279
+ @user = User.find(params[:id])
280
+
281
+ unless @user == current_user
282
+
283
+ redirect_to user_path(current_user)
284
+
285
+ end
286
+
287
+ end
288
+
289
+ end
290
+
291
+
292
+
293
+
294
+
295
+ <部分テンプレート _book_index.html.erb>
296
+
297
+
298
+
299
+ <table class='table table-hover table-inverse'>
300
+
301
+ <thead>
302
+
303
+ <tr>
304
+
305
+ <th></th>
306
+
307
+ <th>Title</th>
308
+
309
+ <th>Opinion</th>
310
+
311
+ <th colspan="3"></th>
312
+
313
+ </tr>
314
+
315
+ </thead>
316
+
317
+ <tbody>
318
+
319
+ <% books.each do |book| %>
320
+
321
+ <tr id="book_<%= book.id %>">
322
+
323
+ <td><%= link_to user_path(book.user) do %>
324
+
325
+ <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-icon.jpg") %>
326
+
327
+ <% end %>
328
+
329
+ </td>
330
+
331
+ <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td>
332
+
333
+ <td><%= book.body %></td>
334
+
335
+ <td class="favorite-btn"><%= render "favorites/favorite-btn", book: book %></td>
336
+
337
+ <td class="comments-count">コメント数: <%= book.book_comments.count %></td>
338
+
339
+ </tr>
340
+
341
+ <% end %>
342
+
343
+ </tbody>
344
+
345
+ </table>
346
+
347
+ <h3>投稿数の前日比・前週比</h3>
348
+
349
+ <div class="table_width">
350
+
351
+ <table class='table table-bordered'>
352
+
353
+ <thead>
354
+
355
+ <tr>
356
+
357
+ <th>今日の投稿数</th>
358
+
359
+ <th>前日の投稿数</th>
360
+
361
+ <th>前日比</th>
362
+
363
+ </tr>
364
+
365
+ </thead>
366
+
367
+ <tbody>
368
+
369
+ <tr>
370
+
371
+ <td><%= @today_book.count %></td>
372
+
373
+ <td><%= @yesterday_book.count %></td>
374
+
375
+ <td>
376
+
377
+ <% if @yesterday_book.count == 0 %>
378
+
379
+ 前日の投稿が0のため計算できません
380
+
381
+ <% else %>
382
+
383
+ <% @the_day_before = @today_book.count / @yesterday_book.count.to_f %>
384
+
385
+ <%= (@the_day_before * 100).round %>%
386
+
387
+ <% end %>
388
+
389
+ </td>
390
+
391
+ </tr>
392
+
393
+ </tbody>
394
+
395
+ </table>
396
+
397
+ </div>
398
+
399
+ <div class="table_width">
400
+
401
+ <table class='table table-bordered'>
402
+
403
+ <thead>
404
+
405
+ <tr>
406
+
407
+ <th>今週の投稿数</th>
408
+
409
+ <th>前週の投稿数</th>
410
+
411
+ <th>前週比</th>
412
+
413
+ </tr>
414
+
415
+ </thead>
416
+
417
+ <tbody>
418
+
419
+ <tr>
420
+
421
+ <td><%= @this_week_book.count %></td>
422
+
423
+ <td><%= @last_week_book.count %></td>
424
+
425
+ <td>
426
+
427
+ <% if @last_week_book.count == 0 %>
428
+
429
+ 前週の投稿が0のため計算できません
430
+
431
+ <% else %>
432
+
433
+ <% @the_week_before = @this_week_book.count / @last_week_book.count.to_f %>
434
+
435
+ <%= (@the_week_before * 100).round %>%
436
+
437
+ <% end %>
438
+
439
+ </td>
440
+
441
+ </tr>
442
+
443
+ </tbody>
444
+
445
+ </table>
446
+
447
+ </div>
448
+
449
+
450
+
451
+ <user/show>
452
+
453
+ <div class='container px-5 px-sm-0'>
454
+
455
+ <div class='row'>
456
+
457
+ <div class='col-md-3'>
458
+
459
+ <h2>User info</h2>
460
+
461
+ <%= render 'info', user: @user %>
462
+
463
+ <h2 class="mt-3">New book</h2>
464
+
465
+ <%= render 'books/form', book: @book %>
466
+
467
+ </div>
468
+
469
+ <div class='col-md-8 offset-md-1'>
470
+
471
+ <h2>Books</h2>
472
+
473
+ <%= render 'book_index',books: @books %>
474
+
475
+ <!--%= current_user.books.where('created_at > ?', Date.today).count %>-->
476
+
477
+ <!--↑↑詳細ページに本日の投稿数を表示-->
478
+
479
+ </div>
480
+
481
+ </div>
482
+
483
+ </div>