ruby-on-rails – 在Activeadmin中删除回形针附件

我正在使用paperclip将图像附件添加到多个模型和Activeadmin以提供简单的管理界面.

我在activeadmin模型文件中有这个代码,允许上传图片

form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Details" do
  f.input :name
  f.input :subdomain
end
f.inputs "General Customisation" do
  f.input :standalone_background,:hint => (("current image:<br/>").html_safe + f.template.image_tag(f.object.standalone_background.url(:thumb))).html_safe,:as => :file
end
end

哪个工作正常.我附加的所有图像都是可选的,因此我想让用户选择删除以前添加的图像,但无法解决如何在Activeadmin中执行此操作.我见过的所有示例都是针对通过单独的has_many关联管理附件而不是主模型的一部分的情况.

有谁知道这样做的方法

解决方法

在您的活动管理视图中
form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Details" do
  f.input :name
  f.input :subdomain
end
f.inputs "General Customisation" do
  f.input :standalone_background,:hint => (("current image:<br/>").html_safe +   f.template.image_tag(f.object.standalone_background.url(:thumb))).html_safe,:as => :file
  f.input :remove_standalone_background,as: :boolean,required: false,label: "remove standalone background"
 end
end

在你的模型中

您可以定义一个状态标志,如波纹管

attr_writer :remove_standalone_background

def remove_standalone_background
  @remove_standalone_background || false
end

或(在轨道3.2中折旧)

attr_accessor_with_default : standalone_background,false

before_save :before_save_callback

def before_save_callback
  if self.remove_standalone_background
    self.remove_standalone_background=nil
  end
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...