带循环和字段的Rails表单,仅用于提交循环的最后一条记录

时间:2019-06-15 02:15:38

标签: ruby-on-rails

更新在底部,谢谢---仍然需要帮助!

Rails 5.2.2 Ruby 4.5

fields_for中的Forms PrintLocation循环仅提交循环的最后一个值。另一个问题是print_file参数为空,如下所示。

我有此表格要更新(创建时存在相同问题):

<%= form_for shop_product do |f| %>
   <%= f.collection_select :product_id, @products, :id, :sku %>
...
   <% PrintLocation.all.each do |print_location| %>
      <%= print_location.title %>
      <%= f.fields_for :shop_product_print_files_attributes do |a| %>
          <%= a.text_field :print_file %>
          <%= a.hidden_field :print_location_id, value: print_location.id %>
          <%= a.hidden_field :shop_product_id, value: shop_product.id %>
      <% end %>
   <% end %>
....

提交的参数:

..."shop_product"=>{"...", "shop_product_print_files_attributes"=>{"print_file"=>"", "print_location_id"=>"6", "shop_product_id"=>"48"}...

print_location_id 6是循环的最后一个值,即使我使用1或1和5,它也将始终作为6提交,并且只有1次。

该表格旨在提交1或最多6个print_file,每个print_location都可以提交一个

型号:

class PrintLocation < ApplicationRecord
  has_many :shop_products, through: :shop_product_print_files
  has_many :shop_product_print_files
  accepts_nested_attributes_for :shop_product_print_files
end

class ShopProductPrintFile < ApplicationRecord
  belongs_to :shop_product
  belongs_to :print_location
end

class ShopProduct < ApplicationRecord
    ...
  has_many :shop_product_print_files
  has_many :print_locations, through: :shop_product_print_files
  accepts_nested_attributes_for :print_locations
  accepts_nested_attributes_for :shop_product_print_files
    ...
end

ShopProduct Controller方法:

  def create
    @shop_product = ShopProduct.new(shop_product_params)
    @shop_product.shop_product_print_files.build(print_location_id: params["shop_product"]["shop_product_print_files_attributes"]["print_location_id"])
    shop = Shop.find(params["shop_product"]["shop_id"])
    product = Product.find(params["shop_product"]["product_id"])
...



    respond_to do |format|
      if @shop_product.save!
        format.html { redirect_to product_variants_path(shopify_product_id: @shop_product.store_product_id, shop_id: @shop_product.shop_id), notice: 'Shop product was successfully updated.' }
        format.json { render :show, status: :created, location: @shop_product }
      else
        format.html { render :new }
        format.json { render json: @shop_product.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @shop_product = ShopProduct.find_by(store_variant_id: params["shop_product"]["store_variant_id"])
    @shop_product.shop_product_print_files.build(shop_product_id: @shop_product.id, print_location_id: params["shop_product"]["shop_product_print_files_attributes"]["print_location_id"])
    @product = Product.find(params["shop_product"]["product_id"])

    respond_to do |format|
      if @shop_product.update(shop_product_params)
        format.html { redirect_to product_variants_path(shopify_product_id: @shop_product.store_product_id, shop_id: @shop_product.shop_id), notice: 'Shop product was successfully updated.' }
        format.json { render :show, status: :ok, location: @shop_product }
      else
        format.html { render :edit }
        format.json { render json: @shop_product.errors, status: :unprocessable_entity }
      end
    end
  end

private

def shop_product_params
   params.require(:shop_product).permit(..., :shop_product_print_files_attributes => [:ids => [], :print_files => [], :print_location_ids => [], :shop_product_ids => []])
end

如何制作表单或控制者,以便表单将print_file与表单中选择的print_location相关联地提交一次,一次最多6个? >

更新:(尽管仍然存在问题) 感谢Rails 5 fields_for only sends last params

将其与以下格式一起使用:

<% PrintLocation.all.each_with_index do |print_location, index| %>
   <%= print_location.title %>
   <%= f.fields_for :shop_product_print_files_attributes, index: index do |a| %>
      <%= a.text_field :print_file %>
      <%= a.hidden_field :print_location_id, value: print_location.id %>
      <%= a.hidden_field :shop_product_id, value: shop_product.id %>
   <% end %>
<% end %>'

允许在提交时输入以下参数:

..."shop_product_print_files_attributes"=>{"0"=>{"print_file"=>"1", "print_location_id"=>"1", "shop_product_id"=>"42"}, "1"=>{"print_file"=>"2", "print_location_id"=>"2", "shop_product_id"=>"42"}, "2"=>{"print_file"=>"", "print_location_id"=>"3", "shop_product_id"=>"42"}, "3"=>{"print_file"=>"", "print_location_id"=>"4", "shop_product_id"=>"42"}, "4"=>{"print_file"=>"", "print_location_id"=>"5", "shop_product_id"=>"42"}, "5"=>{"print_file"=>"", "print_location_id"=>"6", "shop_product_id"=>"42"}},...

我的问题现在是,即使我没有输入print_location,它也会提交所有可能的print_file。在这种形式下,我只提交了print_location == 1print_location == 2print_file分别为“ 1”和“ 2”)。

在这里我该怎么做才能避免全部提交并且只填写print_file的那些提交?

我现在收到的错误是Shop product print files print location must exist,即使我为每个print_file填写了一个print_location

0 个答案:

没有答案