0
我正在使用導軌4和載波波形上傳文件。我有一個產品和資產模型。 Product has_many資產和嵌套表單用於保存產品中的資產。當我更新任何產品時,其資產字段會被重複,當我再次編輯同一產品時,我可以在表單中看到8個字段代替4個字段。更新後,文件字段在導軌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
請幫我解決這個問題。
請更新與'product_params' method.The問題您的文章就在於這一點。 – Pavan 2015-02-24 07:19:05
還要添加你的「編輯」動作? – evanbikes 2015-02-24 07:25:45
嘿,我用params更新了我的答案,並且編輯動作是空的,我仍然在這裏添加它以顯示給所有人。感謝您的迴應。請看一看。 – dips 2015-02-24 07:52:33