【问题标题】:Deleting a Paperclip Attachment in Activeadmin在 Activeadmin 中删除回形针附件
【发布时间】:2017-04-04 13:52:53
【问题描述】:

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

有谁知道这样做的方法吗?

【问题讨论】:

    标签: ruby-on-rails paperclip activeadmin


    【解决方案1】:

    在您的活动管理员视图中

    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
    

    OR(在 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
    

    【讨论】:

    • 你忘了用standalone_background.clear实际删除附件
    【解决方案2】:

    您可以通过创建自定义方法来实现这一点。这是可以做到的

    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
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        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' }
        

        【讨论】:

          【解决方案5】:

          虽然 accepts_nested_attributes_for(:foo, allow_destroy: true) 仅适用于像 belongs_to 这样的 ActiveRecord 关联,但我们可以借鉴其设计,以类似的方式进行回形针附件删除工作。

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

          向已经使用 has_attached_file 的模型添加如下所示的 &lt;attachment_name&gt;_attributes= 编写器方法:

          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
          

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

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

          ActiveAdmin.register YourModelHere do
          
            permit_params :name, :subdomain,
              :standalone_background,
              standalone_background_attributes: [:_destroy]
          

          在同一文件中,将嵌套的 _destroy 复选框添加到 ActiveAdmin form 块。此复选框必须使用 semantic_fields_for(或 formtastic 提供的其他嵌套属性方法之一)嵌套在 &lt;attachment_name&gt;_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/Deleting-Paperclip-Attachments-with-ActiveAdmin

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-23
            • 2016-01-20
            • 1970-01-01
            • 2011-06-14
            • 1970-01-01
            相关资源
            最近更新 更多