保存嵌套表单参数

时间:2019-06-16 03:36:32

标签: ruby-on-rails

嵌套表格参数不会保存到ShopProductPrintFile数据库模型中。

以下参数通过:

NotImplemented

表格:

..., "print_locations"=>{"1"=>{"shop_product_print_files_attributes"=>{"print_file"=>"a", "print_location_id"=>"1"}}, "2"=>{"shop_product_print_files_attributes"=>{"print_file"=>"", "print_location_id"=>"2"}}, "3"=>{"shop_product_print_files_attributes"=>{"print_file"=>"a", "print_location_id"=>"3"}}, "4"=>{"shop_product_print_files_attributes"=>{"print_file"=>"", "print_location_id"=>"4"}}, "5"=>{"shop_product_print_files_attributes"=>{"print_file"=>"", "print_location_id"=>"5"}}, "6"=>{"shop_product_print_files_attributes"=>{"print_file"=>"", "print_location_id"=>"6"}}},...

型号:

...
<% PrintLocation.all.each do |pl| %>
   <%= f.fields_for 'print_locations[]', pl do |ff| %>
      <%= ff.fields_for :shop_product_print_files_attributes do |fff|  %>
      <%= fff.text_field :print_file %>
      <%= fff.hidden_field :print_location_id, value: pl.id %>
      <% end %>
   <% end %>
<% end %>
...

参数:

class ShopProduct < ApplicationRecord

  has_many :shop_product_print_files
  has_many :print_locations, through: :shop_product_print_files
  accepts_nested_attributes_for :shop_product_print_files

end

class ShopProductPrintFile < ApplicationRecord

  belongs_to :shop_product
  belongs_to :print_location
  accepts_nested_attributes_for :print_location

end

class PrintLocation < ApplicationRecord

  has_many :shop_product_print_files
  has_many :shop_products, through: :shop_product_print_files
  accepts_nested_attributes_for :shop_product_print_files

end

我的印象是,只要正确完成了strong_params,这些应该保存下来,我相信它们是吗?还是...?

在创建ShopProduct时,在PrintLocation(标识,名称)模型中设置了6个指定的PrintLocation,可以为每个PrintLocation保存def shop_product_params params.require(:shop_product).permit(:product_id, :store_product_id, :shop_id, :store_variant_id, :sync, :print_file_location, shop_product_print_files_attributes: [ :id, :print_file, :print_location_id, :shop_product_id ] ) end 。然后ShopProductPrintFile模型将容纳print_fileshop_product_idprint_location_id

上面显示的参数后的整个控制台:

print_file

0 个答案:

没有答案