gameは二つのコンテンツを持っていて、その二つのコンテンツをupdateしたいのですがこのように記述すると、新しく二つのコンテンツが作られてしまいます。
すみませんが、わかる方がいらっしゃれば教えてください。宜しくお願いします
ruby
1**gameモデル** 2has_many :contents 3accepts_nested_attributes_for :contents 4**contentモデル** 5belongs_to :game 6 7contents_controller 8 def edit 9 @game = Game.find(params[:id]) 10 @contents = @game.contents 11 end 12 def update 13 binding.pry 14 @game = Game.find(params[:id]) 15 @game.update(cotents_params) 16 end 17 def contents_params 18 params.require(:game).permit(contents_attributes:[:score, :putt, :id]) 19 end
フォームが
html
1= form_for @game,:url => { :controller => :easy_modes, :action => :update }, remote: true do |f| 2 = f.fields_for :contents, @contents do |ff| 3 #js-score-form 4 = ff.label :スコア 5 = ff.number_field :score 6 #js-putt-form 7 = ff.label :パット 8 = ff.number_field :putt 9 = f.submit
binding.pryでのパラメーターが
ruby
1 2 "game"=>{"contents_attributes"=> 3{"0"=>{"score"=>"3", "putt"=>"3", "id"=>"35"} 4, "1"=>{"score"=>"4", "putt"=>"4", "id"=>"36"}}}, "commit"=>"Update Game", "controller"=>"easy_modes", 5 "action"=>"update", "id"=>"103"} permitted: false>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。