美好的一天。 有人在Carrierwave中以嵌套形式删除资产吗?
has_many :article_images, :dependent => :destroy
accepts_nested_attributes_for :article_images
mount_uploader :image, ImageUploader
belongs_to :article, :polymorphic => true
create_table "article_images", :force => true do |t|
t.string "image"
t.string "article_id"
end
create_table "articles", :force => true do |t|
t.string "title"
end
def edit
@article = Article.find(params[:id])
@article.article_images.build
end
_form.html.erb
<%= f.fields_for :article_images do |article_image| %>
<% if article_image.object.new_record? %>
<%= article_image.file_field :image %>
<% else %>
<%= image_tag(article_image.object.image.url(:thumb)) %>
<%= article_image.check_box :remove_image %> #DON'T WORK
<% end %>
<% end %>
答案 0 :(得分:4)
我认为如果你这样做会更好:
class ArticleImage < ActiveRecord::Base
# ...
attr_accessible :remove_image
after_save :clean_remove_image
def changed?
!!(super || remove_image)
end
def clean_remove_image
self.remove_image = nil
end
end
它对我有用。
答案 1 :(得分:2)
如果将此项添加到模型中的accepts_nested_attributes_for,会发生什么:
accepts_nested_attributes_for :article_images, :allow_destroy => true
并在您的视图代码中更改此内容:
<%= article_image.check_box :remove_image %> #DON'T WORK
对此:
<%= article_image.check_box :_destroy %> #MIGHT WORK?