更新后,文件字段在 rails 4 using carrierwave gem 中复制

After updating, file fields are duplicating in rails 4 using carrierwave gem

我正在使用 rails 4 和载波 gem 上传文件。我有一个产品和资产模型。产品 has_many 资产和嵌套表单用于保存产品中的资产。当我更新任何产品时,它的资产字段会重复,当我再次编辑同一产品时,我可以在表单中看到 8 个字段而不是 4 个字段。

Product.rb

 class Product < ActiveRecord::Base

 has_many :assets , :dependent => :delete_all
 accepts_nested_attributes_for :assets 

Asset.rb

class Asset < ActiveRecord::Base
  mount_uploader :asset1, AssetUploader
  mount_uploader :asset2, AssetUploader
  mount_uploader :asset3, AssetUploader
  mount_uploader :asset4, AssetUploader 
end

产品负责人

def new   
  @product = Product.new
  @product.assets.build
end

def create
  @product = Product.new(product_params)
  respond_to do |format|
    if @product.save
      format.html { redirect_to product_product_detail_path(@product), notice: 'Product was successfully created.' }
      format.json { render action: 'show', status: :created, location: @product }
    else
      format.html { render action: 'new' }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end


def update
  respond_to do |format|
    if @product.update(product_params)
      format.html { redirect_to sub_category_path(@product.sub_category_id), notice: 'Product was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

new_html.erb 产品

<%= form_for(@product, html: { multipart: true }) do |f| %>
  <%= f.fields_for :assets do |asset| %>
    <div class="field">
    <%= asset.label :asset, "File #1" %>
    <% unless asset.object.asset1.file.nil? %>
        <%= image_tag(asset.object.asset1.url) %>
        <p><% asset.object.asset1.file.filename %></p>
        <p style="float:left">Change File?</p>
    <% end %>
    <%= asset.file_field :asset1 %>
 </div>

 <div class="field">
    <%= asset.label :asset, "File #2" %>
    <% unless asset.object.asset2.file.nil? %>
        <%= image_tag(asset.object.asset2.url) %>
        <p><% asset.object.asset2.file.filename %></p>
        <p style="float:left">Change File?</p>
    <% end %>
    <%= asset.file_field :asset2 %>
  </div>

  <div class="field">
    <%= asset.label :asset, "File #3" %>
    <% unless asset.object.asset3.file.nil? %>
        <%= image_tag(asset.object.asset3.url) %>
        <p><% asset.object.asset3.file.filename %></p>
        <p style="float:left">Change File?</p>
    <% end %>
    <%= asset.file_field :asset3 %>
  </div>

  <div class="field">
     <%= asset.label :asset, "File #4" %>
     <% unless asset.object.asset4.file.nil? %>
        <%= image_tag(asset.object.asset4.url) %>
        <p><% asset.object.asset4.file.filename %></p>
        <p style="float:left">Change File?</p>
     <% end %>
     <%= asset.file_field :asset4 %>
  </div>
 <% end %>


 <div class="actions">
   <%= f.submit :class=> "action-btn" %>
 </div>
<% end %>

更新-

def product_params
  params.require(:product).permit(assets_attributes: [:asset, :asset1, :asset2, :asset3, :asset4 ])
end

def edit
end

请帮我解决问题。

尝试(用于更新的 id):

def product_params
  params.require(:product).permit(assets_attributes: [:id, :asset, :asset1, :asset2, :asset3, :asset4 ])
end