Reputation: 380
I am using rails 4 and carrierwave gem to uplaod files. I have a product and assets model. Product has_many assets and a nested form is used to save the assets in product. When i update any product, its assets field get duplicated and when I edit same product again I can see 8 fields in the form in place of 4 fields.
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
Product controller
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 for product
<%= 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 %>
UPDATE-
def product_params
params.require(:product).permit(assets_attributes: [:asset, :asset1, :asset2, :asset3, :asset4 ])
end
def edit
end
Please help me to solve the issue.
Upvotes: 0
Views: 205
Reputation:
Try (id for update):
def product_params
params.require(:product).permit(assets_attributes: [:id, :asset, :asset1, :asset2, :asset3, :asset4 ])
end
Upvotes: 1