質問編集履歴

10

ログの修正

2020/10/30 12:21

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -300,31 +300,33 @@
300
300
 
301
301
 
302
302
 
303
- Started POST "/rooms" for ::1 at 2020-10-30 14:18:41 +0900
303
+ Started POST "/rooms" for ::1 at 2020-10-30 21:19:38 +0900
304
304
 
305
305
  Processing by RoomsController#create as HTML
306
306
 
307
- Parameters: {"authenticity_token"=>"QboJSgKi561sC1fc0YvaFeYoqnalb1F2r/lfHSmYD8hLa5FM6DWquLiZTHxExjr+0ZsGp/X2mo7nbl9uSiQ8Sg==", "room"=>{"name"=>"テスト", "user_ids"=>["1", "2"]}, "commit"=>"登録する"}
307
+ Parameters: {"authenticity_token"=>"29rqX0N8xw8O14Yoppch1Eu4jo+rGqHvvwQWeqjWLCbFCeHyK9RMsLsywC8WiRaZbQNh8cOAcOLY1UAuczVQ4Q==", "room"=>{"name"=>"test", "user_ids"=>["2", "1"]}, "commit"=>"登録する"}
308
-
308
+
309
- User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
309
+ User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
310
310
 
311
311
  Unpermitted parameter: :user_ids
312
312
 
313
- (0.1ms) BEGIN
313
+ (0.2ms) BEGIN
314
314
 
315
315
  ↳ app/controllers/rooms_controller.rb:15:in `create'
316
316
 
317
- Room Create (0.2ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('テスト', '2020-10-30 05:18:41.825926', '2020-10-30 05:18:41.825926')
317
+ Room Create (0.3ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('test', '2020-10-30 12:19:38.500111', '2020-10-30 12:19:38.500111')
318
318
 
319
319
  ↳ app/controllers/rooms_controller.rb:15:in `create'
320
320
 
321
- (0.5ms) COMMIT
321
+ (1.9ms) COMMIT
322
322
 
323
323
  ↳ app/controllers/rooms_controller.rb:15:in `create'
324
324
 
325
325
  Redirected to http://localhost:3000/
326
326
 
327
- Completed 302 Found in 8ms (ActiveRecord: 1.3ms | Allocations: 5201)
327
+ Completed 302 Found in 10ms (ActiveRecord: 2.8ms | Allocations: 5200)
328
+
329
+
328
330
 
329
331
 
330
332
 

9

修正

2020/10/30 12:21

投稿

moto12
moto12

スコア15

test CHANGED
@@ -1 +1 @@
1
- Active Hashを付けた後から、中間テーブルに値が入らない (バリデーションに失敗しました: Usersは不正な値です)
1
+ 中間テーブルに値が入らない (バリデーションに失敗しました: Usersは不正な値です)
test CHANGED
@@ -22,13 +22,9 @@
22
22
 
23
23
 
24
24
 
25
-
26
-
27
-
28
-
29
25
  ### 該当のソースコード
30
26
 
31
-
27
+ ```
32
28
 
33
29
  ```Rails
34
30
 

8

修正

2020/10/30 05:27

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -334,4 +334,6 @@
334
334
 
335
335
  ```
336
336
 
337
+ ```
338
+
337
339
  更新日時:10月30日(金) 14:20

7

更新

2020/10/30 05:25

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -12,31 +12,11 @@
12
12
 
13
13
  ### 発生している問題・エラーメッセージ
14
14
 
15
- 初めroomを作成した際、問題なく中間テーブルに値が入っていました。
15
+ roomを作成するroomに入れるuserも選択肢、中間テーブルにroom_idとuser_idを保存した
16
-
16
+
17
- しかし、user登録フォームに、Active Hash用い登録方法に変えたから中間テーブルに値が入らなくなました。
17
+ しかし、room作成ボタン押した後、roomテーブルにのみ値が入ってお、room_usersテーブル(中間テーブル)には
18
-
19
-
20
-
21
- parameterを見たところ、userのid(現在のユーザーと、選択した他のユーザーのid)は値として入っていますが、room_idがnilになっていることが問題なのかもしれません。
18
+
22
-
23
-
24
-
25
- もし、そこが問題ならどのようにroom_idを中間テーブルに入れることができますか?
26
-
27
-
28
-
29
- roomを作成しようとした際出るエラーメッセージは以下の通りです。
30
-
31
-
32
-
33
- ```
34
-
35
- ActiveRecord::RecordInvalid in RoomsController#create
36
-
37
- バリデーションに失敗しました: Usersは不正値です
19
+ 値が入っていい。
38
-
39
-
40
20
 
41
21
  ```
42
22
 
@@ -52,9 +32,7 @@
52
32
 
53
33
  ```Rails
54
34
 
55
- rooms_controller.rb】
35
+ 【new.html.erb】
56
-
57
-
58
36
 
59
37
  class RoomsController < ApplicationController
60
38
 
@@ -74,6 +52,8 @@
74
52
 
75
53
  @room = Room.new
76
54
 
55
+ @room.room_users.build
56
+
77
57
  end
78
58
 
79
59
 
@@ -84,7 +64,7 @@
84
64
 
85
65
  if @room.save
86
66
 
87
- redirect_to root_path (←room作成後、root_pathには戻ります)
67
+ redirect_to root_path
88
68
 
89
69
  else
90
70
 
@@ -114,7 +94,7 @@
114
94
 
115
95
  def room_params
116
96
 
117
- params.require(:room).permit(:name, user_ids: [])
97
+ params.require(:room).permit(:name, room_users_attributes:{user_ids:[]})
118
98
 
119
99
  end
120
100
 
@@ -122,571 +102,215 @@
122
102
 
123
103
  end
124
104
 
125
- ```
126
-
127
-
128
-
129
- ```Rails
130
-
131
- 【room_usersテーブルのマイグレーションファイル】
132
-
133
-
134
-
135
- class CreateRoomUsers < ActiveRecord::Migration[6.0]
136
-
137
- def change
138
-
139
- create_table :room_users do |t|
140
-
141
- t.references :room, foreign_key: true
142
-
143
- t.references :user, foreign_key: true
144
-
145
- t.timestamps
146
-
147
- end
105
+
106
+
107
+ ```
108
+
109
+
110
+
111
+ ```Rails
112
+
113
+ <div class='chat-room-form'>
114
+
115
+ <h1>新規チャットルーム</h1>
116
+
117
+ <%=form_with model: @room, local: true do |f|%>
118
+
119
+ <div class='chat-room-form__field'>
120
+
121
+ <div class='chat-room-form__field--left'>
122
+
123
+ <%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
124
+
125
+ </div>
126
+
127
+ <div class='chat-room-form__field--right'>
128
+
129
+ <%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
130
+
131
+ </div>
132
+
133
+ </div>
134
+
135
+ <div class='chat-room-form__field'>
136
+
137
+ </div>
138
+
139
+ <div class='chat-room-form__field'>
140
+
141
+ <div class='chat-room-form__field--left'>
142
+
143
+ <label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
144
+
145
+ </div>
146
+
147
+ <div class='chat-room-form__field--right'>
148
+
149
+ <select name="room[user_ids][]">
150
+
151
+ <option value="">チャットするユーザーを選択してください</option>
152
+
153
+ <% User.where.not(id: current_user.id).each do |user| %>
154
+
155
+ <option value=<%= user.id %>><%= user.name %></option>
156
+
157
+ <% end %>
158
+
159
+ </select>
160
+
161
+ <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
162
+
163
+ </div>
164
+
165
+ </div>
166
+
167
+ <div class='chat-room-form__field'>
168
+
169
+ <div class='chat-room-form__field--left'></div>
170
+
171
+ <div class='chat-room-form__field--right'>
172
+
173
+ <%= f.submit class: 'chat-room-form__action-btn'%>
174
+
175
+ </div>
176
+
177
+ </div>
178
+
179
+ <% end %>
180
+
181
+ </div>
182
+
183
+ ```
184
+
185
+
186
+
187
+ ```Rails
188
+
189
+ 【room_users.rb(中間テーブル)】
190
+
191
+
192
+
193
+ class RoomUser < ApplicationRecord
194
+
195
+ belongs_to :room, optional: true
196
+
197
+ belongs_to :user, optional: true
198
+
199
+ end
200
+
201
+ ```
202
+
203
+
204
+
205
+ ```Rails
206
+
207
+ 【user.rb】
208
+
209
+
210
+
211
+ class User < ApplicationRecord
212
+
213
+
214
+
215
+ extend ActiveHash::Associations::ActiveRecordExtensions
216
+
217
+
218
+
219
+ devise :database_authenticatable, :registerable,
220
+
221
+ :recoverable, :rememberable, :validatable
222
+
223
+
224
+
225
+ belongs_to_active_hash :genre
226
+
227
+ belongs_to_active_hash :grade
228
+
229
+ belongs_to_active_hash :class_number
230
+
231
+ belongs_to_active_hash :number
232
+
233
+
234
+
235
+ has_many :room_users
236
+
237
+ has_many :rooms, through: :room_users
238
+
239
+ has_many :messages
240
+
241
+ accepts_nested_attributes_for :room_users
242
+
243
+
244
+
245
+ with_options presence: true do
246
+
247
+ validates :name
248
+
249
+ validates :email
250
+
251
+ validates :password
252
+
253
+
254
+
255
+ with_options numericality: { other_than: 1} do
256
+
257
+ validates :genre_id
258
+
259
+ validates :grade_id
260
+
261
+ validates :class_number_id
262
+
263
+ validates :number_id
148
264
 
149
265
  end
150
266
 
267
+ end
268
+
151
269
  end
152
270
 
153
-
154
-
155
- ```
271
+ ```
156
-
157
-
158
-
272
+
273
+
274
+
159
- ```Rails
275
+ ```Rails
160
-
276
+
161
- users_controller.rb】
277
+ 【room.rb】
162
-
163
-
164
-
278
+
279
+
280
+
165
- class UsersController < ApplicationController
281
+ class Room < ApplicationRecord
166
-
167
- def index
282
+
168
-
169
- end
283
+
170
-
171
-
172
-
173
- def new
284
+
174
-
175
- @user = User.new
176
-
177
- end
178
-
179
-
180
-
181
- def create
182
-
183
- @user = User.new(user_params)
184
-
185
- if current_user.save
285
+ has_many :room_users
186
-
286
+
187
- redirect_to root_path
287
+ has_many :users, through: :room_users
188
-
189
- else
288
+
190
-
191
- render :new
289
+ has_many :messages, dependent: :destroy
192
-
193
- end
290
+
194
-
195
- end
196
-
197
-
198
-
199
- def edit
200
-
201
- end
202
-
203
-
204
-
205
- def update
206
-
207
- if current_user.update(user_params)
291
+ accepts_nested_attributes_for :room_users
208
-
209
- redirect_to root_path
292
+
210
-
211
- else
212
-
213
- render :edit
214
-
215
- end
216
-
217
- end
218
-
219
-
220
-
221
- private
222
-
223
-
224
-
225
- def user_params
226
-
227
- params.require(:user).permit(
293
+ validates :name, presence: true
228
-
229
- :name,
230
-
231
- :email,
232
-
233
- :password,
234
-
235
- :password_confirm,
236
-
237
- :genre_id,
238
-
239
- :grade_id,
240
-
241
- :number_id,
242
-
243
- :class_number_id
244
-
245
- )
246
-
247
- end
248
294
 
249
295
  end
250
296
 
251
-
252
-
253
- ```
254
-
255
-
256
-
257
- ```Rails
258
-
259
- 【room.rb】
260
-
261
-
262
-
263
- class Room < ApplicationRecord
264
-
265
-
266
-
267
- has_many :room_users
268
-
269
- has_many :users, through: :room_users
270
-
271
- has_many :messages, dependent: :destroy
272
-
273
-
274
-
275
- validates :name, presence: true
276
-
277
- end
278
-
279
-
280
-
281
- ```
282
-
283
-
284
-
285
- ```Rails
286
-
287
- 【user.rb】
288
-
289
-
290
-
291
- class User < ApplicationRecord
292
-
293
-
294
-
295
- extend ActiveHash::Associations::ActiveRecordExtensions
296
-
297
-
298
-
299
- devise :database_authenticatable, :registerable,
300
-
301
- :recoverable, :rememberable, :validatable
302
-
303
-
304
-
305
- belongs_to_active_hash :genre
306
-
307
- belongs_to_active_hash :grade
308
-
309
- belongs_to_active_hash :class_number
310
-
311
- belongs_to_active_hash :number
312
-
313
-
314
-
315
- has_many :room_users
316
-
317
- has_many :rooms, through: :room_users
318
-
319
- has_many :messages
320
-
321
-
322
-
323
- with_options presence: true do
324
-
325
- validates :name
326
-
327
- validates :email
328
-
329
- validates :password
330
-
331
-
332
-
333
- with_options numericality: { other_than: 1} do
334
-
335
- validates :genre_id
336
-
337
- validates :grade_id
338
-
339
- validates :class_number_id
340
-
341
- validates :number_id
342
-
343
- end
344
-
345
- end
346
-
347
- end
348
-
349
-
350
-
351
- ```
352
-
353
-
354
-
355
- ```Rails
356
-
357
- 【room_users.rb】
358
-
359
-
360
-
361
- class RoomUser < ApplicationRecord
362
-
363
- belongs_to :room, optional: true
364
-
365
- belongs_to :user, optional: true
366
-
367
- end
368
-
369
-
370
-
371
- ```
372
-
373
-
374
-
375
-
376
-
377
- ### 試したこと
378
-
379
- サイトで調べた結果、中間テーブルにある、user_idとroom_idがbigintになっているのを、integerに変更したが、特別変化は起きていない。(現在はまたbigintに戻している)
380
-
381
-
382
-
383
-
384
-
385
- ### 補足情報(FW/ツールのバージョンなど)
386
-
387
- 【binding.pry後、@roomの値を確認した結果】
388
-
389
-
390
-
391
- 12: def create
392
-
393
- 13: @room = Room.new(room_params)
394
-
395
- => 14: binding.pry
396
-
397
- 15: if @room.save!
398
-
399
- 16: redirect_to root_path
400
-
401
- 17: else
402
-
403
- 18: redirect_to new_room_path
404
-
405
- 19: end
406
-
407
- 20: end
408
-
409
-
410
-
411
- [1] pry(#<RoomsController>)> @room
412
-
413
- => #<Room:0x00007f855aaac5a8 id: nil, name: "新規作成", created_at: nil, updated_at: nil>
414
-
415
-
416
-
417
- ```Rails
418
-
419
- 【(room)new.html.erb】
420
-
421
-
422
-
423
- <div class='chat-room-form'>
424
-
425
- <h1>新規チャットルーム</h1>
426
-
427
- <%=form_with model: @room, local: true do |f|%>
428
-
429
- <div class='chat-room-form__field'>
430
-
431
- <div class='chat-room-form__field--left'>
432
-
433
- <%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
434
-
435
- </div>
436
-
437
- <div class='chat-room-form__field--right'>
438
-
439
- <%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
440
-
441
- </div>
442
-
443
- </div>
444
-
445
- <div class='chat-room-form__field'>
446
-
447
- </div>
448
-
449
- <div class='chat-room-form__field'>
450
-
451
- <div class='chat-room-form__field--left'>
452
-
453
- <label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
454
-
455
- </div>
456
-
457
- <div class='chat-room-form__field--right'>
458
-
459
- <select name="room[user_ids][]">
460
-
461
- <option value="">チャットするユーザーを選択してください</option>
462
-
463
- <% User.where.not(id: current_user.id).each do |user| %>
464
-
465
- <option value=<%= user.id %>><%= user.name %></option>
466
-
467
- <% end %>
468
-
469
- </select>
470
-
471
- <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
472
-
473
- </div>
474
-
475
- </div>
476
-
477
- <div class='chat-room-form__field'>
478
-
479
- <div class='chat-room-form__field--left'></div>
480
-
481
- <div class='chat-room-form__field--right'>
482
-
483
- <%= f.submit class: 'chat-room-form__action-btn'%>
484
-
485
- </div>
486
-
487
- </div>
488
-
489
- <% end %>
490
-
491
- </div>
492
-
493
- ```
494
-
495
-
496
-
497
- ```Rails
498
-
499
- class RoomsController < ApplicationController
500
-
501
-
502
-
503
-
504
-
505
- def index
506
-
507
-
508
-
509
- end
510
-
511
-
512
-
513
- def new
514
-
515
- @room = Room.new
516
-
517
- @room.room_users.build
518
-
519
- end
520
-
521
-
522
-
523
- def create
524
-
525
- @room = Room.new(room_params)
526
-
527
- if @room.save
528
-
529
- # binding.pry
530
-
531
- redirect_to root_path
532
-
533
- else
534
-
535
- redirect_to new_room_path
536
-
537
- end
538
-
539
- end
540
-
541
-
542
-
543
- def destroy
544
-
545
- room = Room.find(params[:id])
546
-
547
- room.destroy
548
-
549
- redirect_to root_path
550
-
551
- end
552
-
553
-
554
-
555
- private
556
-
557
-
558
-
559
- def room_params
560
-
561
- params.require(:room).permit(:name, user_ids_attributes: [])
562
-
563
- end
564
-
565
-
566
-
567
- end
568
-
569
- ```
570
-
571
-
572
-
573
-
574
-
575
- 10月28日(水) 17:22 更新
576
-
577
- ```Rails
578
-
579
- <div class='chat-room-form'>
580
-
581
- <h1>新規チャットルーム</h1>
582
-
583
- <%=form_with model: @room, local: true do |f|%>
584
-
585
- <div class='chat-room-form__field'>
586
-
587
- <div class='chat-room-form__field--left'>
588
-
589
- <%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
590
-
591
- </div>
592
-
593
- <div class='chat-room-form__field--right'>
594
-
595
- <%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
596
-
597
- </div>
598
-
599
- </div>
600
-
601
- <div class='chat-room-form__field'>
602
-
603
- </div>
604
-
605
- <div class='chat-room-form__field'>
606
-
607
- <div class='chat-room-form__field--left'>
608
-
609
- <label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
610
-
611
- </div>
612
-
613
- <div class='chat-room-form__field--right'>
614
-
615
- <select name="room[user_ids][]">
616
-
617
- <option value="">チャットするユーザーを選択してください</option>
618
-
619
- <% User.where.not(id: current_user.id).each do |user| %>
620
-
621
- <option value=<%= user.id %>><%= user.name %></option>
622
-
623
- <% end %>
624
-
625
- </select>
626
-
627
- <%# <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>> %>
628
-
629
- </div>
630
-
631
- </div>
632
-
633
- <div class='chat-room-form__field'>
634
-
635
- <div class='chat-room-form__field--left'></div>
636
-
637
- <div class='chat-room-form__field--right'>
638
-
639
- <%= f.submit class: 'chat-room-form__action-btn'%>
640
-
641
- </div>
642
-
643
- </div>
644
-
645
- <% end %>
646
-
647
- </div>
648
-
649
- ```
650
-
651
-
652
-
653
- ```Rails
654
-
655
- class Room < ApplicationRecord
656
-
657
-
658
-
659
- has_many :room_users
660
-
661
- has_many :users, through: :room_users
662
-
663
- has_many :messages, dependent: :destroy
664
-
665
- accepts_nested_attributes_for :room_users
666
-
667
- validates :name, presence: true
668
-
669
- end
670
-
671
- ```
672
-
673
-
674
-
675
- 更新 10月28日(水) 20:58
676
-
677
-
678
-
679
- room作成ボタンを押した際の、現在のログの状況
680
-
681
- ```Rails
682
-
683
- Started POST "/rooms" for ::1 at 2020-10-28 20:47:17 +0900
297
+ ```
298
+
299
+
300
+
301
+ ```Rails
302
+
303
+ 【ログ】
304
+
305
+
306
+
307
+ Started POST "/rooms" for ::1 at 2020-10-30 14:18:41 +0900
684
308
 
685
309
  Processing by RoomsController#create as HTML
686
310
 
687
- Parameters: {"authenticity_token"=>"HWt4O6MU9KiYhIPCzbdoCK73UT9ID61wWCn3rjOj/M4XuuA9SYO5vUwWmGJY+ojjmUT97hiWZogQvvfdUB/PTA==", "room"=>{"name"=>"新規作成", "user_ids"=>["1", "2"]}, "commit"=>"登録する"}
311
+ Parameters: {"authenticity_token"=>"QboJSgKi561sC1fc0YvaFeYoqnalb1F2r/lfHSmYD8hLa5FM6DWquLiZTHxExjr+0ZsGp/X2mo7nbl9uSiQ8Sg==", "room"=>{"name"=>"テスト", "user_ids"=>["1", "2"]}, "commit"=>"登録する"}
688
-
312
+
689
- User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
313
+ User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
690
314
 
691
315
  Unpermitted parameter: :user_ids
692
316
 
@@ -694,18 +318,20 @@
694
318
 
695
319
  ↳ app/controllers/rooms_controller.rb:15:in `create'
696
320
 
697
- Room Create (0.3ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('新規作成', '2020-10-28 11:47:17.906618', '2020-10-28 11:47:17.906618')
321
+ Room Create (0.2ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('テスト', '2020-10-30 05:18:41.825926', '2020-10-30 05:18:41.825926')
698
322
 
699
323
  ↳ app/controllers/rooms_controller.rb:15:in `create'
700
324
 
701
- (0.3ms) COMMIT
325
+ (0.5ms) COMMIT
702
326
 
703
327
  ↳ app/controllers/rooms_controller.rb:15:in `create'
704
328
 
705
329
  Redirected to http://localhost:3000/
706
330
 
707
- Completed 302 Found in 8ms (ActiveRecord: 1.1ms | Allocations: 5182)
331
+ Completed 302 Found in 8ms (ActiveRecord: 1.3ms | Allocations: 5201)
708
-
709
-
710
-
332
+
333
+
334
+
711
- ```
335
+ ```
336
+
337
+ 更新日時:10月30日(金) 14:20

6

追記

2020/10/30 05:22

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -672,20 +672,40 @@
672
672
 
673
673
 
674
674
 
675
+ 更新 10月28日(水) 20:58
676
+
677
+
678
+
675
- room作成ボタンを押した際の、現在のターミナルの状況
679
+ room作成ボタンを押した際の、現在のログの状況
676
-
680
+
677
- ```Rails
681
+ ```Rails
678
-
682
+
679
- Started GET "/" for ::1 at 2020-10-28 17:19:39 +0900
683
+ Started POST "/rooms" for ::1 at 2020-10-28 20:47:17 +0900
680
-
684
+
681
- Processing by RoomsController#index as HTML
685
+ Processing by RoomsController#create as HTML
686
+
682
-
687
+ Parameters: {"authenticity_token"=>"HWt4O6MU9KiYhIPCzbdoCK73UT9ID61wWCn3rjOj/M4XuuA9SYO5vUwWmGJY+ojjmUT97hiWZogQvvfdUB/PTA==", "room"=>{"name"=>"新規作成", "user_ids"=>["1", "2"]}, "commit"=>"登録する"}
688
+
683
- User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
689
+ User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
690
+
684
-
691
+ Unpermitted parameter: :user_ids
692
+
693
+ (0.1ms) BEGIN
694
+
685
- Rendering rooms/index.html.erb within layouts/application
695
+ ↳ app/controllers/rooms_controller.rb:15:in `create'
686
-
696
+
687
- Room Load (0.3ms) SELECT `rooms`.* FROM `rooms` INNER JOIN `room_users` ON `rooms`.`id` = `room_users`.`room_id` WHERE `room_users`.`user_id` = 2
697
+ Room Create (0.3ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('新規作成', '2020-10-28 11:47:17.906618', '2020-10-28 11:47:17.906618')
698
+
688
-
699
+ ↳ app/controllers/rooms_controller.rb:15:in `create'
700
+
689
-
701
+ (0.3ms) COMMIT
702
+
690
-
703
+ ↳ app/controllers/rooms_controller.rb:15:in `create'
704
+
705
+ Redirected to http://localhost:3000/
706
+
707
+ Completed 302 Found in 8ms (ActiveRecord: 1.1ms | Allocations: 5182)
708
+
709
+
710
+
691
- ```
711
+ ```

5

追記

2020/10/28 11:58

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -572,7 +572,7 @@
572
572
 
573
573
 
574
574
 
575
- 10月28日(水) 17:18 更新
575
+ 10月28日(水) 17:22 更新
576
576
 
577
577
  ```Rails
578
578
 
@@ -624,7 +624,7 @@
624
624
 
625
625
  </select>
626
626
 
627
- <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
627
+ <%# <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>> %>
628
628
 
629
629
  </div>
630
630
 
@@ -669,3 +669,23 @@
669
669
  end
670
670
 
671
671
  ```
672
+
673
+
674
+
675
+ room作成ボタンを押した際の、現在のターミナルの状況
676
+
677
+ ```Rails
678
+
679
+ Started GET "/" for ::1 at 2020-10-28 17:19:39 +0900
680
+
681
+ Processing by RoomsController#index as HTML
682
+
683
+ User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
684
+
685
+ Rendering rooms/index.html.erb within layouts/application
686
+
687
+ Room Load (0.3ms) SELECT `rooms`.* FROM `rooms` INNER JOIN `room_users` ON `rooms`.`id` = `room_users`.`room_id` WHERE `room_users`.`user_id` = 2
688
+
689
+
690
+
691
+ ```

4

追記

2020/10/28 08:23

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -491,3 +491,181 @@
491
491
  </div>
492
492
 
493
493
  ```
494
+
495
+
496
+
497
+ ```Rails
498
+
499
+ class RoomsController < ApplicationController
500
+
501
+
502
+
503
+
504
+
505
+ def index
506
+
507
+
508
+
509
+ end
510
+
511
+
512
+
513
+ def new
514
+
515
+ @room = Room.new
516
+
517
+ @room.room_users.build
518
+
519
+ end
520
+
521
+
522
+
523
+ def create
524
+
525
+ @room = Room.new(room_params)
526
+
527
+ if @room.save
528
+
529
+ # binding.pry
530
+
531
+ redirect_to root_path
532
+
533
+ else
534
+
535
+ redirect_to new_room_path
536
+
537
+ end
538
+
539
+ end
540
+
541
+
542
+
543
+ def destroy
544
+
545
+ room = Room.find(params[:id])
546
+
547
+ room.destroy
548
+
549
+ redirect_to root_path
550
+
551
+ end
552
+
553
+
554
+
555
+ private
556
+
557
+
558
+
559
+ def room_params
560
+
561
+ params.require(:room).permit(:name, user_ids_attributes: [])
562
+
563
+ end
564
+
565
+
566
+
567
+ end
568
+
569
+ ```
570
+
571
+
572
+
573
+
574
+
575
+ 10月28日(水) 17:18 更新
576
+
577
+ ```Rails
578
+
579
+ <div class='chat-room-form'>
580
+
581
+ <h1>新規チャットルーム</h1>
582
+
583
+ <%=form_with model: @room, local: true do |f|%>
584
+
585
+ <div class='chat-room-form__field'>
586
+
587
+ <div class='chat-room-form__field--left'>
588
+
589
+ <%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
590
+
591
+ </div>
592
+
593
+ <div class='chat-room-form__field--right'>
594
+
595
+ <%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
596
+
597
+ </div>
598
+
599
+ </div>
600
+
601
+ <div class='chat-room-form__field'>
602
+
603
+ </div>
604
+
605
+ <div class='chat-room-form__field'>
606
+
607
+ <div class='chat-room-form__field--left'>
608
+
609
+ <label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
610
+
611
+ </div>
612
+
613
+ <div class='chat-room-form__field--right'>
614
+
615
+ <select name="room[user_ids][]">
616
+
617
+ <option value="">チャットするユーザーを選択してください</option>
618
+
619
+ <% User.where.not(id: current_user.id).each do |user| %>
620
+
621
+ <option value=<%= user.id %>><%= user.name %></option>
622
+
623
+ <% end %>
624
+
625
+ </select>
626
+
627
+ <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
628
+
629
+ </div>
630
+
631
+ </div>
632
+
633
+ <div class='chat-room-form__field'>
634
+
635
+ <div class='chat-room-form__field--left'></div>
636
+
637
+ <div class='chat-room-form__field--right'>
638
+
639
+ <%= f.submit class: 'chat-room-form__action-btn'%>
640
+
641
+ </div>
642
+
643
+ </div>
644
+
645
+ <% end %>
646
+
647
+ </div>
648
+
649
+ ```
650
+
651
+
652
+
653
+ ```Rails
654
+
655
+ class Room < ApplicationRecord
656
+
657
+
658
+
659
+ has_many :room_users
660
+
661
+ has_many :users, through: :room_users
662
+
663
+ has_many :messages, dependent: :destroy
664
+
665
+ accepts_nested_attributes_for :room_users
666
+
667
+ validates :name, presence: true
668
+
669
+ end
670
+
671
+ ```

3

new.html.erbの追加

2020/10/28 08:18

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -411,3 +411,83 @@
411
411
  [1] pry(#<RoomsController>)> @room
412
412
 
413
413
  => #<Room:0x00007f855aaac5a8 id: nil, name: "新規作成", created_at: nil, updated_at: nil>
414
+
415
+
416
+
417
+ ```Rails
418
+
419
+ 【(room)new.html.erb】
420
+
421
+
422
+
423
+ <div class='chat-room-form'>
424
+
425
+ <h1>新規チャットルーム</h1>
426
+
427
+ <%=form_with model: @room, local: true do |f|%>
428
+
429
+ <div class='chat-room-form__field'>
430
+
431
+ <div class='chat-room-form__field--left'>
432
+
433
+ <%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
434
+
435
+ </div>
436
+
437
+ <div class='chat-room-form__field--right'>
438
+
439
+ <%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
440
+
441
+ </div>
442
+
443
+ </div>
444
+
445
+ <div class='chat-room-form__field'>
446
+
447
+ </div>
448
+
449
+ <div class='chat-room-form__field'>
450
+
451
+ <div class='chat-room-form__field--left'>
452
+
453
+ <label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
454
+
455
+ </div>
456
+
457
+ <div class='chat-room-form__field--right'>
458
+
459
+ <select name="room[user_ids][]">
460
+
461
+ <option value="">チャットするユーザーを選択してください</option>
462
+
463
+ <% User.where.not(id: current_user.id).each do |user| %>
464
+
465
+ <option value=<%= user.id %>><%= user.name %></option>
466
+
467
+ <% end %>
468
+
469
+ </select>
470
+
471
+ <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
472
+
473
+ </div>
474
+
475
+ </div>
476
+
477
+ <div class='chat-room-form__field'>
478
+
479
+ <div class='chat-room-form__field--left'></div>
480
+
481
+ <div class='chat-room-form__field--right'>
482
+
483
+ <%= f.submit class: 'chat-room-form__action-btn'%>
484
+
485
+ </div>
486
+
487
+ </div>
488
+
489
+ <% end %>
490
+
491
+ </div>
492
+
493
+ ```

2

追記

2020/10/28 05:10

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -84,11 +84,111 @@
84
84
 
85
85
  if @room.save
86
86
 
87
+ redirect_to root_path (←room作成後、root_pathには戻ります)
88
+
89
+ else
90
+
91
+ redirect_to new_room_path
92
+
93
+ end
94
+
95
+ end
96
+
97
+
98
+
99
+ def destroy
100
+
101
+ room = Room.find(params[:id])
102
+
103
+ room.destroy
104
+
105
+ redirect_to root_path
106
+
107
+ end
108
+
109
+
110
+
111
+ private
112
+
113
+
114
+
115
+ def room_params
116
+
117
+ params.require(:room).permit(:name, user_ids: [])
118
+
119
+ end
120
+
121
+
122
+
123
+ end
124
+
125
+ ```
126
+
127
+
128
+
129
+ ```Rails
130
+
131
+ 【room_usersテーブルのマイグレーションファイル】
132
+
133
+
134
+
135
+ class CreateRoomUsers < ActiveRecord::Migration[6.0]
136
+
137
+ def change
138
+
139
+ create_table :room_users do |t|
140
+
141
+ t.references :room, foreign_key: true
142
+
143
+ t.references :user, foreign_key: true
144
+
145
+ t.timestamps
146
+
147
+ end
148
+
149
+ end
150
+
151
+ end
152
+
153
+
154
+
155
+ ```
156
+
157
+
158
+
159
+ ```Rails
160
+
161
+ 【users_controller.rb】
162
+
163
+
164
+
165
+ class UsersController < ApplicationController
166
+
167
+ def index
168
+
169
+ end
170
+
171
+
172
+
173
+ def new
174
+
175
+ @user = User.new
176
+
177
+ end
178
+
179
+
180
+
181
+ def create
182
+
183
+ @user = User.new(user_params)
184
+
185
+ if current_user.save
186
+
87
187
  redirect_to root_path
88
188
 
89
189
  else
90
190
 
91
- redirect_to new_room_path
191
+ render :new
92
192
 
93
193
  end
94
194
 
@@ -96,13 +196,23 @@
96
196
 
97
197
 
98
198
 
99
- def destroy
199
+ def edit
200
+
100
-
201
+ end
202
+
203
+
204
+
205
+ def update
206
+
101
- room = Room.find(params[:id])
207
+ if current_user.update(user_params)
102
-
103
- room.destroy
208
+
104
-
105
- redirect_to root_path
209
+ redirect_to root_path
210
+
211
+ else
212
+
213
+ render :edit
214
+
215
+ end
106
216
 
107
217
  end
108
218
 
@@ -112,236 +222,126 @@
112
222
 
113
223
 
114
224
 
115
- def room_params
116
-
117
- params.require(:room).permit(:name, user_ids: [])
118
-
119
- end
120
-
121
-
122
-
123
- end
124
-
125
- ```
126
-
127
-
128
-
129
- ```Rails
130
-
131
- 【room_usersテーブルのマイグレーションファイル】
132
-
133
-
134
-
135
- class CreateRoomUsers < ActiveRecord::Migration[6.0]
136
-
137
- def change
138
-
139
- create_table :room_users do |t|
140
-
141
- t.references :room, foreign_key: true
142
-
143
- t.references :user, foreign_key: true
144
-
145
- t.timestamps
146
-
147
- end
225
+ def user_params
226
+
227
+ params.require(:user).permit(
228
+
229
+ :name,
230
+
231
+ :email,
232
+
233
+ :password,
234
+
235
+ :password_confirm,
236
+
237
+ :genre_id,
238
+
239
+ :grade_id,
240
+
241
+ :number_id,
242
+
243
+ :class_number_id
244
+
245
+ )
246
+
247
+ end
248
+
249
+ end
250
+
251
+
252
+
253
+ ```
254
+
255
+
256
+
257
+ ```Rails
258
+
259
+ 【room.rb】
260
+
261
+
262
+
263
+ class Room < ApplicationRecord
264
+
265
+
266
+
267
+ has_many :room_users
268
+
269
+ has_many :users, through: :room_users
270
+
271
+ has_many :messages, dependent: :destroy
272
+
273
+
274
+
275
+ validates :name, presence: true
276
+
277
+ end
278
+
279
+
280
+
281
+ ```
282
+
283
+
284
+
285
+ ```Rails
286
+
287
+ 【user.rb】
288
+
289
+
290
+
291
+ class User < ApplicationRecord
292
+
293
+
294
+
295
+ extend ActiveHash::Associations::ActiveRecordExtensions
296
+
297
+
298
+
299
+ devise :database_authenticatable, :registerable,
300
+
301
+ :recoverable, :rememberable, :validatable
302
+
303
+
304
+
305
+ belongs_to_active_hash :genre
306
+
307
+ belongs_to_active_hash :grade
308
+
309
+ belongs_to_active_hash :class_number
310
+
311
+ belongs_to_active_hash :number
312
+
313
+
314
+
315
+ has_many :room_users
316
+
317
+ has_many :rooms, through: :room_users
318
+
319
+ has_many :messages
320
+
321
+
322
+
323
+ with_options presence: true do
324
+
325
+ validates :name
326
+
327
+ validates :email
328
+
329
+ validates :password
330
+
331
+
332
+
333
+ with_options numericality: { other_than: 1} do
334
+
335
+ validates :genre_id
336
+
337
+ validates :grade_id
338
+
339
+ validates :class_number_id
340
+
341
+ validates :number_id
148
342
 
149
343
  end
150
344
 
151
- end
152
-
153
-
154
-
155
- ```
156
-
157
-
158
-
159
- ```Rails
160
-
161
- 【users_controller.rb】
162
-
163
-
164
-
165
- class UsersController < ApplicationController
166
-
167
- def index
168
-
169
- end
170
-
171
-
172
-
173
- def new
174
-
175
- @user = User.new
176
-
177
- end
178
-
179
-
180
-
181
- def create
182
-
183
- @user = User.new(user_params)
184
-
185
- if current_user.save
186
-
187
- redirect_to root_path
188
-
189
- else
190
-
191
- render :new
192
-
193
- end
194
-
195
- end
196
-
197
-
198
-
199
- def edit
200
-
201
- end
202
-
203
-
204
-
205
- def update
206
-
207
- if current_user.update(user_params)
208
-
209
- redirect_to root_path
210
-
211
- else
212
-
213
- render :edit
214
-
215
- end
216
-
217
- end
218
-
219
-
220
-
221
- private
222
-
223
-
224
-
225
- def user_params
226
-
227
- params.require(:user).permit(
228
-
229
- :name,
230
-
231
- :email,
232
-
233
- :password,
234
-
235
- :password_confirm,
236
-
237
- :genre_id,
238
-
239
- :grade_id,
240
-
241
- :number_id,
242
-
243
- :class_number_id
244
-
245
- )
246
-
247
- end
248
-
249
- end
250
-
251
-
252
-
253
- ```
254
-
255
-
256
-
257
- ```Rails
258
-
259
- 【room.rb】
260
-
261
-
262
-
263
- class Room < ApplicationRecord
264
-
265
-
266
-
267
- has_many :room_users
268
-
269
- has_many :users, through: :room_users
270
-
271
- has_many :messages, dependent: :destroy
272
-
273
-
274
-
275
- validates :name, presence: true
276
-
277
- end
278
-
279
-
280
-
281
- ```
282
-
283
-
284
-
285
- ```Rails
286
-
287
- 【user.rb】
288
-
289
-
290
-
291
- class User < ApplicationRecord
292
-
293
-
294
-
295
- extend ActiveHash::Associations::ActiveRecordExtensions
296
-
297
-
298
-
299
- devise :database_authenticatable, :registerable,
300
-
301
- :recoverable, :rememberable, :validatable
302
-
303
-
304
-
305
- belongs_to_active_hash :genre
306
-
307
- belongs_to_active_hash :grade
308
-
309
- belongs_to_active_hash :class_number
310
-
311
- belongs_to_active_hash :number
312
-
313
-
314
-
315
- has_many :room_users
316
-
317
- has_many :rooms, through: :room_users
318
-
319
- has_many :messages
320
-
321
-
322
-
323
- with_options presence: true do
324
-
325
- validates :name
326
-
327
- validates :email
328
-
329
- validates :password
330
-
331
-
332
-
333
- with_options numericality: { other_than: 1} do
334
-
335
- validates :genre_id
336
-
337
- validates :grade_id
338
-
339
- validates :class_number_id
340
-
341
- validates :number_id
342
-
343
- end
344
-
345
345
  end
346
346
 
347
347
  end

1

追記

2020/10/28 03:06

投稿

moto12
moto12

スコア15

test CHANGED
File without changes
test CHANGED
@@ -384,6 +384,30 @@
384
384
 
385
385
  ### 補足情報(FW/ツールのバージョンなど)
386
386
 
387
-
387
+ 【binding.pry後、@roomの値を確認した結果】
388
+
389
+
390
+
388
-
391
+ 12: def create
392
+
393
+ 13: @room = Room.new(room_params)
394
+
395
+ => 14: binding.pry
396
+
389
- ここにより詳細な情報を記載してください。
397
+ 15: if @room.save!
398
+
399
+ 16: redirect_to root_path
400
+
401
+ 17: else
402
+
403
+ 18: redirect_to new_room_path
404
+
405
+ 19: end
406
+
407
+ 20: end
408
+
409
+
410
+
411
+ [1] pry(#<RoomsController>)> @room
412
+
413
+ => #<Room:0x00007f855aaac5a8 id: nil, name: "新規作成", created_at: nil, updated_at: nil>