我再次需要你的帮助。现在我需要了解如何使用 Carrierwave 上传的文件(在我的情况下 - 图像)中删除。
模型/附件.rb:
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
attr_accessible :file, :file
mount_uploader :file, FileUploader
end
模型/post.rb:
class Post < ActiveRecord::Base
attr_accessible :content, :title, :attachments_attributes, :_destroy
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments
end
*意见/帖子/_form.html.erb :*
<%= nested_form_for @post, :html=>{:multipart => true } do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="field">
<%= f.label :Nosaukums %>:<br /><br />
<%= f.text_field :title %><br /><br />
</div>
<div id="field">
<%= f.label :Raksts %>:<br /><br />
<%= f.text_area :content %><br /><br />
</div>
<%= f.fields_for :attachments do |attachment| %>
<% if attachment.object.new_record? %>
<%= attachment.file_field :file %>
<% else %>
<%= image_tag(attachment.object.file.url) %>
<%= f.check_box :_destroy %>
<% end %>
<% end %>
<%= f.submit "Publicēt", :id => "button-link" %>
<% end %>
当我尝试删除以前上传的文件时,出现此错误:
unknown attribute: _destroy
也许有问题,因为我有多个文件上传,而不仅仅是一个。