質問編集履歴

1

モデル名が複数形になっていた為修正しました。Concertのコントローラーで@programを作っていなかった為newの部分に足しました。pry-byebugの出力内容を追記しました。

2020/02/04 06:21

投稿

midoriya
midoriya

スコア7

test CHANGED
File without changes
test CHANGED
@@ -90,7 +90,7 @@
90
90
 
91
91
  <label>曲目</label>
92
92
 
93
- <%= f.fields_for :programs do |builder| %>
93
+ <%= f.fields_for :program do |builder| %>
94
94
 
95
95
  <%= render 'program_fields', f: builder %>
96
96
 
@@ -122,15 +122,15 @@
122
122
 
123
123
 
124
124
 
125
- <%= f.fields_for :musics do |music| %>
125
+ <%= f.fields_for :music do |m| %>
126
-
127
-
128
-
126
+
127
+
128
+
129
- <div class="form-group">
129
+ <div class="form-group">
130
-
130
+
131
- <%= music.label :曲名 %>
131
+ <%= m.label :曲名 %>
132
-
132
+
133
- <%= music.text_field :title, class: 'form-control' %>
133
+ <%= m.text_field :title, class: 'form-control' %>
134
134
 
135
135
  </div>
136
136
 
@@ -138,9 +138,9 @@
138
138
 
139
139
  <div class="form-group">
140
140
 
141
- <%= music.label :作曲家 %>
141
+ <%= m.label :作曲家 %>
142
-
142
+
143
- <%= music.text_field :composer, class: 'form-control' %>
143
+ <%= m.text_field :composer, class: 'form-control' %>
144
144
 
145
145
  </div>
146
146
 
@@ -192,11 +192,11 @@
192
192
 
193
193
  belongs_to :user
194
194
 
195
- has_many :musics, through: :programs
195
+ has_many :music, through: :program
196
-
196
+
197
- has_many :programs, dependent: :destroy
197
+ has_many :program, dependent: :destroy
198
-
198
+
199
- accepts_nested_attributes_for :programs, allow_destroy: true
199
+ accepts_nested_attributes_for :program, allow_destroy: true
200
200
 
201
201
 
202
202
 
@@ -228,9 +228,9 @@
228
228
 
229
229
  class Music < ApplicationRecord
230
230
 
231
- has_many :programs, dependent: :destroy
231
+ has_many :program, dependent: :destroy
232
-
232
+
233
- has_many :concerts, through: :programs
233
+ has_many :concert, through: :program
234
234
 
235
235
 
236
236
 
@@ -280,7 +280,7 @@
280
280
 
281
281
  @concert = Concert.new(flash[:user])
282
282
 
283
- @concert.programs.build
283
+ @program = @concert.program.build
284
284
 
285
285
  end
286
286
 
@@ -322,9 +322,7 @@
322
322
 
323
323
  def show
324
324
 
325
- @concertt = Concert.find_by(id: params[:id])
325
+ @concert = Concert.find_by(id: params[:id])
326
-
327
- @user = @concert.user
328
326
 
329
327
  end
330
328
 
@@ -336,7 +334,7 @@
336
334
 
337
335
  def concert_params
338
336
 
339
- params.require(:concert).permit(:datetime, :title, :venue,programs_attributes:[:id, :concert_id, :music_id, :_destroy, music_attributes:[:title, :composer]])
337
+ params.require(:concert).permit(:datetime, :title, :venue,program_attributes:[:id, :concert_id, :music_id, :_destroy, music_attribute:[:title, :composer, :_destroy]])
340
338
 
341
339
  end
342
340
 
@@ -346,12 +344,110 @@
346
344
 
347
345
 
348
346
 
349
-
350
-
351
-
352
-
353
347
  ### 試したこと
354
348
 
355
349
  出力されたhtmlを見て、f.submitがきちんとform要素の中にいるのは確認しました
356
350
 
357
351
  エラーメッセージも出ないので.saveする時点でこけてるような気がしますが、原因がわかりません……。
352
+
353
+
354
+
355
+ ###追記
356
+
357
+
358
+
359
+ ###pry-byebugの出力内容
360
+
361
+ Started GET "/concerts/new" at 2020-02-04 06:11:55 +0000
362
+
363
+ Cannot render console
364
+
365
+ Processing by ConcertsController#new as HTML
366
+
367
+ User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
368
+
369
+ ↳ app/controllers/application_controller.rb:9
370
+
371
+ Rendering concerts/new.html.erb within layouts/application
372
+
373
+ Rendered shared/_error_messages.html.erb (0.4ms)
374
+
375
+ Rendered concerts/_program_fields.html.erb (1.9ms)
376
+
377
+ Rendered concerts/_program_fields.html.erb (1.8ms)
378
+
379
+ Rendered concerts/_form.html.erb (44.1ms)
380
+
381
+ Rendered concerts/new.html.erb within layouts/application (55.6ms)
382
+
383
+ Rendered application/_header.html.erb (1.3ms)
384
+
385
+ Completed 200 OK in 476ms (Views: 413.0ms | ActiveRecord: 4.8ms)
386
+
387
+
388
+
389
+
390
+
391
+ Started POST "/concerts"
392
+
393
+ Cannot render console
394
+
395
+ Processing by ConcertsController#create as JS
396
+
397
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"+1nH06/4mrERyuUTB7RQBURNbfDpxiEIei15iRSMb3UnAiYbZhBH6uDnjKHYDxgTGbAJ8frWsI8PqZp5KXM+Zw==", "concert"=>{"title"=>"演奏会名1", "datetime(1i)"=>"2020", "datetime(2i)"=>"2", "datetime(3i)"=>"4", "datetime(4i)"=>"06", "datetime(5i)"=>"11", "venue"=>"会場1", "pr_comment"=>"コメント1", "program_attributes"=>{"0"=>{"music"=>{"title"=>"曲名1", "composer"=>"作曲家名1"}, "_destroy"=>"false"}, "1580796739696"=>{"music"=>{"title"=>"曲名2", "composer"=>"作曲家名2"}, "_destroy"=>"false"}}}, "commit"=>"保存"}
398
+
399
+ User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1
400
+
401
+ ↳ app/controllers/application_controller.rb:9
402
+
403
+ Unpermitted parameter: :music
404
+
405
+ Unpermitted parameter: :music
406
+
407
+
408
+
409
+ From: /app/app/controllers/concerts_controller.rb @ line 17 ConcertsController#create:
410
+
411
+
412
+
413
+ 11: def create
414
+
415
+ 12: concert = Concert.new(concert_params)
416
+
417
+ 13:
418
+
419
+ 14: binding.pry
420
+
421
+ 15:
422
+
423
+ 16:
424
+
425
+ => 17: if concert.save
426
+
427
+ 18: redirect_to @concert
428
+
429
+ 19: else
430
+
431
+ 20: redirect_back fallback_location: concerts_path, flash: {
432
+
433
+ 21: concert: concert,
434
+
435
+ 22: error_messages: concert.errors.full_messages
436
+
437
+ 23: }
438
+
439
+ 24: end
440
+
441
+ 25: end
442
+
443
+
444
+
445
+ [1] pry(#<ConcertsController>)> concert_params
446
+
447
+ Unpermitted parameter: :music
448
+
449
+ Unpermitted parameter: :music
450
+
451
+ => <ActionController::Parameters {"datetime(1i)"=>"2020", "datetime(2i)"=>"2", "datetime(3i)"=>"4", "datetime(4i)"=>"06", "datetime(5i)"=>"11", "title"=>"演奏会名1", "venue"=>"会場1", "pr_comment"=>"コメント1", "program_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"_destroy"=>"false"} permitted: true>, "1580796739696"=><ActionController::Parameters {"_destroy"=>"false"} permitted: true>} permitted: true>} permitted: true>
452
+
453
+ [2] pry(#<ConcertsController>)>