2012-02-11 15 views
6

में पेपरक्लिप अटैचमेंट को हटाने से मैं एक सरल व्यवस्थापक इंटरफ़ेस प्रदान करने के लिए कई मॉडलों और 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 मॉडल फ़ाइल में इस कोड को जो छवि अपलोड के लिए अनुमति देता है। इस तरह से संलग्न सभी छवियां वैकल्पिक हैं और इसलिए मैं उपयोगकर्ता को पहले जोड़ा गया चित्र हटाने का विकल्प देना चाहता हूं लेकिन यह सक्रिय नहीं है कि इसे Activeadmin में कैसे किया जाए। मैंने जो उदाहरण देखा है, वह उन परिस्थितियों के लिए है जहां संलग्नक मुख्य मॉडल का हिस्सा होने के बजाय अलग है Imany एसोसिएशन के माध्यम से प्रबंधित किए जाते हैं।

क्या कोई इसे करने का तरीका जानता है?

उत्तर

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 

में

आप bellow

attr_writer :remove_standalone_background 

def remove_standalone_background 
    @remove_standalone_background || false 
end 

या

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

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) केवल belongs_to तरह ActiveRecord संघों के साथ काम करता है हम अपने डिजाइन से उधार ले सकते हैं ताकि पेपरक्लिप अटैचमेंट डिलीशन काम इसी तरह से हो सके।

(समझने के लिए कैसे नेस्टेड विशेषताओं के काम में रेल देखने http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).

अपने मॉडल पहले से ही उपयोग करता है has_attached_file के लिए नीचे की तरह एक <attachment_name>_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 

<attachment_name>_attributes= प्रणाली को बुलाती है Paperclip::Attachment#clear चिह्नित करने के लिए मॉडल को अगली सहेजी जाने पर विनाश के लिए लगाव।

अगलाखोलें ,

ActiveAdmin.register YourModelHere do 

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

एक ही फाइल में ActiveAdmin form ब्लॉक करने के लिए एक नेस्टेड _destroy चेकबॉक्स जोड़ें:फ़ाइल (अपने अनुप्रयोग के लिए सही फ़ाइल पथ का उपयोग) और सेटअप मजबूत मापदंडों <attachment_name>_attributes पर _destroy झंडा नेस्ट विशेषता अनुमति देने के लिए। यह चेकबॉक्स <attachment_name>_attributessemantic_fields_for (या फॉर्मेटस्टिक द्वारा प्रदान किए गए अन्य नेस्टेड विशेषताओं विधियों में से एक) का उपयोग करके घोंसला होना चाहिए।

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