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

时间:2015-02-24 07:17:32

标签: ruby-on-rails carrierwave

我正在使用rails 4和carrierwave 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

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

尝试(更新ID):

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