在 Activeadmin 中删除回形针附件

发布于 2025-01-04 19:21:52 字数 635 浏览 1 评论 0原文

我使用回形针将图像附件添加到多个模型,并使用 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 关联进行管理的情况,而不是作为主模型的一部分。

有谁知道有什么方法可以做到这一点?

I'm using paperclip to add image attachments to several models and Activeadmin to provide a simple admin interface.

I have this code in my activeadmin model file which allows for image uploads:

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

which works fine. All of the images I'm attaching like this are optional and so I'd like to give the user the option to remove a previously added image but can't work out how to do this in Activeadmin. All of the example I've seen are for situations where the attachments are managed through a separate has_many association rather than being part of the main model.

Does anyone know of a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

胡渣熟男 2025-01-11 19:21:52

在您的活动管理视图中

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

或(在 Rails 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

In your active admin view

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

In your model

You could define a status flag like bellow

attr_writer :remove_standalone_background

def remove_standalone_background
  @remove_standalone_background || false
end

OR (depreciated in rails 3.2)

attr_accessor_with_default : standalone_background,false

before_save :before_save_callback

And

def before_save_callback
  if self.remove_standalone_background
    self.remove_standalone_background=nil
  end
end
笛声青案梦长安 2025-01-11 19:21:52

您可以通过创建自定义方法来实现这一点。 您还应该

member_action :custom_action, :method => :get do
//code
end

添加一个带有链接的自定义列,例如

index do
  column "Custom" do |item|
    link_to "Custom action", "/admin/items/custom_action"
  end
end

You could implement this by creating a custom method. This can be done

member_action :custom_action, :method => :get do
//code
end

Also you should add a custom column with a link such as

index do
  column "Custom" do |item|
    link_to "Custom action", "/admin/items/custom_action"
  end
end
养猫人 2025-01-11 19:21:52

另一种选择是为附件或图像添加状态标志。在保存编辑的对象之前,请取消链接图像。

Another option is to have a status flag for the attachment or image. Before saving the edited object, you unlink the image.

我要还你自由 2025-01-11 19:21:52

谢谢你们的帮助。这是最终的工作代码...

admin/product.rb

f.input :image, required: false, hint: (("Current image:<br/>").html_safe + f.template.image_tag(f.object.image.url(:thumb))).html_safe
f.input :remove_image, as: :boolean, required: false, label: "Remove Image"

models/product.rb

attr_writer :remove_image

def remove_image
  @remove_image || false
end

before_validation { self.image.clear if self.remove_image == '1' }

Thank you for your help guys. This is the final working code...

admin/product.rb

f.input :image, required: false, hint: (("Current image:<br/>").html_safe + f.template.image_tag(f.object.image.url(:thumb))).html_safe
f.input :remove_image, as: :boolean, required: false, label: "Remove Image"

models/product.rb

attr_writer :remove_image

def remove_image
  @remove_image || false
end

before_validation { self.image.clear if self.remove_image == '1' }
花开雨落又逢春i 2025-01-11 19:21:52

尽管accepts_nested_attributes_for(:foo,allow_destroy: true)仅适用于ActiveRecord关联,例如belongs_to,但我们可以借鉴其设计,以类似的方式删除回形针附件。

(要了解嵌套属性在 Rails 中的工作原理,请参阅 http: //api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)。

添加_attributes= writer 方法如下所示,添加到已使用 has_attached_file 的模型:

has_attached_file :standalone_background

def standalone_background_attributes=(attributes)
  # Marks the attachment for destruction on next save,
  # if the attributes hash contains a _destroy flag
  # and a new file was not uploaded at the same time:
  if has_destroy_flag?(attributes) && !standalone_background.dirty?
    standalone_background.clear
  end
end

_attributes= 方法调用 Paperclip::Attachment#clear 标记附件以便在下次保存模型时销毁。

接下来打开现有的 app/admin/your_model_here.rb 文件(为您的应用使用正确的文件路径)并设置强参数以允许 _destroy 标志嵌套属性在 _attributes

ActiveAdmin.register YourModelHere do

  permit_params :name, :subdomain,
    :standalone_background,
    standalone_background_attributes: [:_destroy]

在同一文件中,将嵌套的 _destroy 复选框添加到 ActiveAdmin form 块中。此复选框必须使用 semantic_fields_for (或 formattastic 提供的其他嵌套属性方法之一)嵌套在 _attributes 中。

form :html => { :enctype => "multipart/form-data"} do |f|
  f.inputs "Details" do
    ...
  end
  f.inputs "General Customisation" do
    ...
    if f.object.standalone_background.present?
      f.semantic_fields_for :standalone_background_attributes do |fields|
        fields.input :_destroy, as: :boolean, label: 'Delete?'
      end
    end
  end
end

当存在附件时,您的表单现在应该显示一个删除复选框。选中此复选框并提交有效的表单应该会删除附件。

来源: https://github.com/activeadmin/activeadmin/ wiki/使用 ActiveAdmin 删除回形针附件

Although accepts_nested_attributes_for(:foo, allow_destroy: true) only works with ActiveRecord associations like belongs_to we can borrow from its design to have paperclip attachment deletion work in a similar way.

(To understand how nested attributes work in Rails see http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).

Add a <attachment_name>_attributes= writer method like below to your model that already uses has_attached_file:

has_attached_file :standalone_background

def standalone_background_attributes=(attributes)
  # Marks the attachment for destruction on next save,
  # if the attributes hash contains a _destroy flag
  # and a new file was not uploaded at the same time:
  if has_destroy_flag?(attributes) && !standalone_background.dirty?
    standalone_background.clear
  end
end

The <attachment_name>_attributes= method calls Paperclip::Attachment#clear to mark the attachment for destruction when the model is next saved.

Next open the existing app/admin/your_model_here.rb file (use the correct file path for your app) and setup strong parameters to permit the _destroy flag nested attribute on <attachment_name>_attributes:

ActiveAdmin.register YourModelHere do

  permit_params :name, :subdomain,
    :standalone_background,
    standalone_background_attributes: [:_destroy]

In the same file, add a nested _destroy checkbox to the ActiveAdmin form block. This checkbox must be nested within <attachment_name>_attributes using semantic_fields_for (or one of the other nested attributes methods provided by formtastic).

form :html => { :enctype => "multipart/form-data"} do |f|
  f.inputs "Details" do
    ...
  end
  f.inputs "General Customisation" do
    ...
    if f.object.standalone_background.present?
      f.semantic_fields_for :standalone_background_attributes do |fields|
        fields.input :_destroy, as: :boolean, label: 'Delete?'
      end
    end
  end
end

Your form should now show a delete checkbox when there is an attachment present. Checking this checkbox and submitting a valid form ought to delete the attachment.

Source: https://github.com/activeadmin/activeadmin/wiki/Deleting-Paperclip-Attachments-with-ActiveAdmin

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文