2012-01-25 39 views
3

我跟着Railcast#253 http://railscasts.com/episodes/253-carrierwave-file-uploads工作很好。但後來我用ActiveAdmin實現了它,因此Formtastic(ActiveAdmin使用Formtastic作爲表單)。Rails 3 Carrierwave如何刪除是模型屬性的文件?

所以我能夠上傳文件和下載文件。

問題是,當鏈接到刪除鏈接時,似乎Carrierwave期望模型而不是模型的屬性。

我有模型貨件,其中有hbl_pdf屬性(PDF文檔)。這是我的刪除鏈接...

row("HBL") { link_to 'remove', shipment.hbl_pdf, :confirm => "Are you sure?", :method => :delete } 

我得到錯誤... undefined method model_name for PdfUploader:Class

我不想刪除貨,只有文檔。

回答

12

爲什麼不是在貨件對象上調用remove_hbl_pdf!?閱讀這個「刪除上傳的文件」部分:https://github.com/jnicklas/carrierwave#readme

link_to 'remove', remove_shipment_pdf_path(shipment), :confirm => "Are you sure?", :method => :delete 

,並在控制器

def remove_shipment_pdf 
    shipment = Shipment.find_by_id(params[:shipment_id]) 
    shipment.remove_hbl_pdf! if shipment 
    # respond with something or redirect 
end 

的刪除命令的語法是根據你的屬性的名稱。所以如果你的型號屬性名稱是.hbl_pdf,那麼它是remove_hbl_pdf!,如果它是.image那麼它將是remove_image!

+0

比我的好多了! – nodrog