2012-02-11 19 views
6

Tôi đang sử dụng kẹp giấy để thêm tệp đính kèm hình ảnh vào một số kiểu máy và Activeadmin để cung cấp giao diện quản trị đơn giản.Xóa Tệp đính kèm kẹp giấy trong Activeadmin

tôi có mã này trong file mô hình activeadmin của tôi mà cho phép bạn upload hình ảnh:

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 

mà hoạt động tốt. Tất cả các hình ảnh tôi đính kèm như thế này là tùy chọn và vì vậy tôi muốn cung cấp cho người dùng tùy chọn xóa hình ảnh đã thêm trước đó nhưng không thể tìm ra cách thực hiện điều này trong Activeadmin. Tất cả các ví dụ tôi đã thấy là cho các tình huống mà các tập tin đính kèm được quản lý thông qua một hiệp hội has_many riêng biệt hơn là một phần của mô hình chính.

Có ai biết cách để thực hiện việc này không?

Trả lời

1

Bạn có thể thực hiện việc này bằng cách tạo phương thức tùy chỉnh. Điều này có thể được thực hiện

member_action :custom_action, :method => :get do 
//code 
end 

Ngoài ra, bạn nên thêm một cột tùy chỉnh với một liên kết như

index do 
    column "Custom" do |item| 
    link_to "Custom action", "/admin/items/custom_action" 
    end 
end 
1

Một lựa chọn khác là phải có một lá cờ trạng thái cho các tập tin đính kèm hoặc hình ảnh. Trước khi lưu đối tượng đã chỉnh sửa, bạn hủy liên kết hình ảnh.

2

Trong hoạt động xem admin của bạn

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 

Trong mô hình của bạn

Bạn có thể định nghĩa một lá cờ tình trạng như dưới đây

attr_writer :remove_standalone_background 

def remove_standalone_background 
    @remove_standalone_background || false 
end 

OR (khấu hao trong đường ray 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 
+0

Bạn quên xóa tệp đính kèm với standalone_background.clear – kars7e

1

Cảm ơn sự giúp đỡ của các bạn. Đây là đoạn code làm việc cuối cùng ...

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" 

mô hình/product.rb

attr_writer :remove_image 

def remove_image 
    @remove_image || false 
end 

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

Mặc dù accepts_nested_attributes_for(:foo, allow_destroy: true) chỉ làm việc với các hiệp hội ActiveRecord như belongs_to chúng ta có thể mượn từ thiết kế của nó để xóa tập tin đính kèm kẹp giấy theo cách tương tự.

(Để hiểu cách lồng các thuộc tính làm việc trong Rails thấy http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).

Thêm một phương pháp <attachment_name>_attributes= nhà văn như dưới đây để mô hình của bạn mà đã sử dụng 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 

Phương pháp <attachment_name>_attributes= gọi Paperclip::Attachment#clear để đánh dấu đính kèm để hủy diệt khi mô hình được lưu tiếp theo.

Tiếp theo mởhiện tạifile (sử dụng đường dẫn tập tin chính xác cho ứng dụng của bạn) và thiết lập các thông số mạnh mẽ cho phép _destroy cờ lồng thuộc tính trên <attachment_name>_attributes:

ActiveAdmin.register YourModelHere do 

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

Trong cùng một tập tin, thêm một _destroy hộp lồng nhau để khối ActiveAdmin form. Hộp kiểm này phải được lồng trong phạm vi <attachment_name>_attributes bằng cách sử dụng semantic_fields_for (hoặc một trong các phương thức thuộc tính lồng nhau khác do formtastic cung cấp).

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 

Biểu mẫu của bạn giờ đây sẽ hiển thị hộp kiểm xóa khi có tệp đính kèm. Chọn hộp kiểm này và gửi biểu mẫu hợp lệ phải xóa tệp đính kèm.

Nguồn: https://github.com/activeadmin/activeadmin/wiki/Deleting-Paperclip-Attachments-with-ActiveAdmin