हालांकि 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>_attributes
semantic_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
स्रोत
2017-04-04 05:47:26
आप वास्तव में standalone_background.clear साथ लगाव को हटाने के लिए भूल गया – kars7e