ruby-on-rails - 如何通过carrierwave删除nested_form中的asset?

标签 ruby-on-rails nested-forms carrierwave nested-attributes

美好的一天。 有人在 Carrierwave 中以嵌套形式删除 Assets 的工作解决方案吗?

型号

has_many :article_images, :dependent => :destroy    
accepts_nested_attributes_for :article_images

mount_uploader :image, ImageUploader    
belongs_to :article, :polymorphic => true

架构.rb

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

Controller

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 %>

最佳答案

我认为你这样做会更好:

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

它对我有用。

关于ruby-on-rails - 如何通过carrierwave删除nested_form中的asset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9954754/

相关文章:

ruby-on-rails - Rails - 不稳定的超时错误可能与 Assets 编译有关

javascript - 将 Rails 中的记录分组为图表?

javascript - 提交嵌套表单,奇怪的行为

ruby-on-rails - 在不暴露整个路径的情况下显示 Rails Carrierwave URL

ruby-on-rails - rails simple_form 阻止选择默认添加空白到集合

html - 可链接的 <Div> 叠加在 iframe 视频上 | rails

ruby-on-rails - 访问 accepts_nested_attributes_for 表单中的对象值

javascript - Railscast #197 的问题 - 嵌套模型表单第 2 部分

ruby-on-rails - store_path 中的 Carrierwave : error with model. id

ruby-on-rails - Carrierwave:本地镜像的权限被拒绝