前提・実現したいこと
Ruby on rails 5.2.3
演奏会情報を投稿するシステムを作っています。
ConcertとMusicのモデルについて、Programの中間テーブルを作り多対多の関係づけをし、
一つのConcertの投稿に複数の曲(Music)を登録して投稿できるフォームを実装しようとしていました。
登録する曲の数を動的に変えられるようにしたいため、cocoon(1.2.14)を使用しています。
発生している問題・エラーメッセージ
createアクションが反応しなくなりました
該当のソースコード
ruby
1_form.html.erb 2 3<%= render 'shared/error_messages' %> 4 5<%= form_with model: concert do |f| %> 6 7 <div class="form-group"> 8 <%= f.label :name, 'コンサート名' %> 9 <%= f.text_field :title, class: 'form-control' %> 10 </div> 11 12 <div class="form-group"> 13 <%= f.label :datetime, '開催日' %> 14 <%= f.datetime_select :datetime, class: 'form-control' %> 15 </div> 16 17 <div class="form-group"> 18 <%= f.label :venue, '会場' %> 19 <%= f.text_field :venue, class: 'form-control' %> 20 </div> 21 22 <div class="form-group"> 23 <%= f.label :pr_comment, 'PRコメント' %> 24 <%= f.text_area :pr_comment, class: 'form-control' %> 25 </div> 26 27 <div class="form-group"> 28 <label>曲目</label> 29 <%= f.fields_for :program do |builder| %> 30 <%= render 'program_fields', f: builder %> 31 <div id="links"> 32 <%= link_to_add_association "曲を追加", f, :programs, class: 'btn btn-secondary' %> 33 <% end %> 34 </div> 35 36 <%= f.submit '保存', class: 'btn btn-primary' %> 37 <% end %>
ruby
1_program_fields.html.erb 2 3<div class="nested-fields form-inline"> 4 5<%= f.fields_for :music do |m| %> 6 7 <div class="form-group"> 8 <%= m.label :曲名 %> 9 <%= m.text_field :title, class: 'form-control' %> 10 </div> 11 12 <div class="form-group"> 13 <%= m.label :作曲家 %> 14 <%= m.text_field :composer, class: 'form-control' %> 15 </div> 16 17<% end %> 18 <%= link_to_remove_association '削除', f, class: 'btn btn-danger' %> 19 20</div>
ruby
1# == Schema Information 2# 3# Table name: concerts 4# 5# id :bigint not null, primary key 6# datetime :datetime 7# pr_comment :text(65535) 8# title :string(255) 9# venue :string(255) 10# created_at :datetime not null 11# updated_at :datetime not null 12# user_id :bigint 13# 14 15class Concert < ApplicationRecord 16 belongs_to :user 17 has_many :music, through: :program 18 has_many :program, dependent: :destroy 19 accepts_nested_attributes_for :program, allow_destroy: true 20 21end 22 23# == Schema Information 24# 25# Table name: musics 26# 27# id :bigint not null, primary key 28# composer :string(255) not null 29# title :string(255) not null 30# created_at :datetime not null 31# updated_at :datetime not null 32# 33 34class Music < ApplicationRecord 35 has_many :program, dependent: :destroy 36 has_many :concert, through: :program 37 38end 39 40# == Schema Information 41# 42# Table name: programs 43# 44# id :bigint not null, primary key 45# created_at :datetime not null 46# updated_at :datetime not null 47# concert_id :bigint 48# music_id :bigint 49 50class Program < ApplicationRecord 51 belongs_to :concert 52 belongs_to :music 53end 54
ruby
1class ConcertsController < ApplicationController 2 def new 3 @concert = Concert.new(flash[:user]) 4 @program = @concert.program.build 5 end 6 7 def create 8 concert = Concert.new(concert_params) 9 10 if concert.save 11 redirect_to @concert 12 else 13 redirect_back fallback_location: concerts_path, flash: { 14 concert: concert, 15 error_messages: concert.errors.full_messages 16 } 17 end 18 end 19 20 def index 21 @concerts = Concert.all.order(created_at: :desc) 22 end 23 24 def show 25 @concert = Concert.find_by(id: params[:id]) 26 end 27 28 private 29 30 def concert_params 31 params.require(:concert).permit(:datetime, :title, :venue,program_attributes:[:id, :concert_id, :music_id, :_destroy, music_attribute:[:title, :composer, :_destroy]]) 32 end 33end
試したこと
出力されたhtmlを見て、f.submitがきちんとform要素の中にいるのは確認しました
エラーメッセージも出ないので.saveする時点でこけてるような気がしますが、原因がわかりません……。
###追記
###pry-byebugの出力内容
Started GET "/concerts/new" at 2020-02-04 06:11:55 +0000
Cannot render console
Processing by ConcertsController#new as HTML
User Load (0.7ms) SELECT users
.* FROM users
WHERE users
.id
= 3 LIMIT 1
↳ app/controllers/application_controller.rb:9
Rendering concerts/new.html.erb within layouts/application
Rendered shared/_error_messages.html.erb (0.4ms)
Rendered concerts/_program_fields.html.erb (1.9ms)
Rendered concerts/_program_fields.html.erb (1.8ms)
Rendered concerts/_form.html.erb (44.1ms)
Rendered concerts/new.html.erb within layouts/application (55.6ms)
Rendered application/_header.html.erb (1.3ms)
Completed 200 OK in 476ms (Views: 413.0ms | ActiveRecord: 4.8ms)
Started POST "/concerts"
Cannot render console
Processing by ConcertsController#create as JS
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"=>"保存"}
User Load (0.9ms) SELECT users
.* FROM users
WHERE users
.id
= 3 LIMIT 1
↳ app/controllers/application_controller.rb:9
Unpermitted parameter: :music
Unpermitted parameter: :music
From: /app/app/controllers/concerts_controller.rb @ line 17 ConcertsController#create:
11: def create 12: concert = Concert.new(concert_params) 13: 14: binding.pry 15: 16:
=> 17: if concert.save
18: redirect_to @concert
19: else
20: redirect_back fallback_location: concerts_path, flash: {
21: concert: concert,
22: error_messages: concert.errors.full_messages
23: }
24: end
25: end
[1] pry(#<ConcertsController>)> concert_params
Unpermitted parameter: :music
Unpermitted parameter: :music
=> <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>
[2] pry(#<ConcertsController>)>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/05 03:10
2020/02/05 03:14
2020/02/05 03:16