2012-02-11 56 views
6

我用回形針圖像附件添加到多個模型,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關聯來管理附件,而不是主要模型的一部分。

有誰知道一種方法來做到這一點?

回答

1

您可以通過創建一個自定義的方法實現這一點。這是可以做到

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 
1

另一種選擇是有附件或圖像的狀態標誌。保存編輯的對象之前,請取消鏈接圖像。

2

在你主動管理視圖

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

attr_accessor_with_default : standalone_background,false 

before_save :before_save_callback 
(在軌3.2折舊)的狀態標誌

def before_save_callback 
    if self.remove_standalone_background 
    self.remove_standalone_background=nil 
    end 
end 
+0

你忘了真正刪除與standalone_background.clear附件 – kars7e 2013-09-02 22:16:49

1

謝謝你的幫助球員。這是最後的工作代碼...

管理/ 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" 

型號/ product.rb

attr_writer :remove_image 

def remove_image 
    @remove_image || false 
end 

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

雖然accepts_nested_attributes_for(:foo, allow_destroy: true)只適用於ActiveRecord的協會,如belongs_to我們可以借用它的設計來以類似的方式刪除回形針附件。

(要了解屬性是如何嵌套在工作中的Rails看到http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).

像下面添加<attachment_name>_attributes=作家方法已經使用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 

<attachment_name>_attributes=方法調用Paperclip::Attachment#clear紀念當模型下次保存時附件銷燬

接下來打開現有的app/admin/your_model_here.rb文件(使用正確的文件路徑爲您的應用程序),並建立強大的參數,以允許_destroy標誌嵌套屬性上<attachment_name>_attributes

ActiveAdmin.register YourModelHere do 

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

在同一文件中,添加一個嵌套_destroy複選框的ActiveAdmin form塊。此複選框必須使用semantic_fields_for(或由formtastic提供的其他嵌套屬性方法之一)嵌套在<attachment_name>_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