仅在选中框时创建记录

时间:2017-02-28 10:50:36

标签: ruby-on-rails

以下PATCH操作获取以下参数

  Parameters: {"utf8"=>"✓", [...], "contractgroup"=>
{"articolocontractgroups_attributes"=>
{"0"=>{"articolo_id"=>"0", "id"=>"425"}, 
 "1"=>{"articolo_id"=>"true", "id"=>"426"}, 
 "2"=>{"articolo_id"=>"true", "id"=>"427"}, 
 "3"=>{"articolo_id"=>"0", "id"=>"428"}}, "contractgroup_id"=>"2"}, "commit"=>"Add"}

但是控制器操作是以错误的方式调用参数,因为没有创建记录:

params[:contractgroup][:articolocontractgroups_attributes].each do |id, attrs|
  if params[:articolo_id] == "true"
    @articolocontractgroup = Articolocontractgroup.new
     (articolo_id: params[:contractgroup][:articolo_id], 
      contractgroup_id: params[:contractgroup][:contractgroup_id])
    @articolocontractgroup.save
  end
end

这搞砸了?

1 个答案:

答案 0 :(得分:2)

您需要检查attrs[:articolo_id] == "true"而不是params[:articolo_id]