在 Activeadmin 中删除回形针附件
我使用回形针将图像附件添加到多个模型,并使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的活动管理视图中
在您的模型中
您可以定义一个状态标志,如下所示
或(在 Rails 3.2 中已弃用)
并且
In your active admin view
In your model
You could define a status flag like bellow
OR (depreciated in rails 3.2)
And
您可以通过创建自定义方法来实现这一点。 您还应该
添加一个带有链接的自定义列,例如
You could implement this by creating a custom method. This can be done
Also you should add a custom column with a link such as
另一种选择是为附件或图像添加状态标志。在保存编辑的对象之前,请取消链接图像。
Another option is to have a status flag for the attachment or image. Before saving the edited object, you unlink the image.
谢谢你们的帮助。这是最终的工作代码...
admin/product.rb
models/product.rb
Thank you for your help guys. This is the final working code...
admin/product.rb
models/product.rb
尽管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
的模型:_attributes=
方法调用Paperclip::Attachment#clear
标记附件以便在下次保存模型时销毁。接下来打开现有的
app/admin/your_model_here.rb
文件(为您的应用使用正确的文件路径)并设置强参数以允许_destroy
标志嵌套属性在_attributes
:在同一文件中,将嵌套的
_destroy
复选框添加到 ActiveAdminform
块中。此复选框必须使用semantic_fields_for
(或 formattastic 提供的其他嵌套属性方法之一)嵌套在_attributes
中。当存在附件时,您的表单现在应该显示一个删除复选框。选中此复选框并提交有效的表单应该会删除附件。
来源: https://github.com/activeadmin/activeadmin/ wiki/使用 ActiveAdmin 删除回形针附件
Although
accepts_nested_attributes_for(:foo, allow_destroy: true)
only works with ActiveRecord associations likebelongs_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 useshas_attached_file
:The
<attachment_name>_attributes=
method callsPaperclip::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
:In the same file, add a nested
_destroy
checkbox to the ActiveAdminform
block. This checkbox must be nested within<attachment_name>_attributes
usingsemantic_fields_for
(or one of the other nested attributes methods provided by formtastic).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