加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:13:33 所属栏目:百科 来源:网络整理
导读:我正在使用paperclip将图像附件添加到多个模型和Activeadmin以提供简单的管理界面. 我在activeadmin模型文件中有这个代码,允许上传图片: form :html = { :enctype = "multipart/form-data"} do |f|f.inputs "Details" do f.input :name f.input :subdomaine
我正在使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读