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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在Activeadmin中删除回形针附件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用paperclip将图像附件添加到多个模型和Activeadmin以提供简单的管理界面.

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

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

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

有谁知道这样做的方法

解决方法

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

在你的模型中

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

  1. attr_writer :remove_standalone_background
  2.  
  3. def remove_standalone_background
  4. @remove_standalone_background || false
  5. end

或(在轨道3.2中折旧)

  1. attr_accessor_with_default : standalone_background,false
  2.  
  3. before_save :before_save_callback

  1. def before_save_callback
  2. if self.remove_standalone_background
  3. self.remove_standalone_background=nil
  4. end
  5. end

猜你在找的Ruby相关文章