Carrierwave多文件上传 - 活动管理员

时间:2016-02-18 13:15:56

标签: ruby-on-rails ruby activeadmin carrierwave

我有一个页面可以添加附件。我想在浏览过程中添加多个文件,所以我添加了:multiple =>我的输入上的true方法,因此在此输入期间看起来像这样:f.has_many:attachments

f.input :file , :input_html => { :multiple => true }

要在附件面板上添加/查看多个附件,我有:

attachment=[]

(0..params[:kayako_client_ticket][:attachment].count-1).each do |f|
 attachment << {:filename => params[:kayako_client_ticket][:attachment]      
 [f].original_filename,:data => File.new(params[:kayako_client_ticket] 
 [:attachment][f].tempfile).read}
end unless params[:kayako_client_ticket][:attachment].blank?

在我的模型文件中,附件的引用如下:

has_many :attachments, :as => :attachable, :dependent => :destroy

如果我添加多个文件,我会收到此错误:

You are not allowed to upload nil files, allowed types: jpg, jpeg, gif, png, doc, docx, xls, xlsx, xlsb, csv, txt, ppt, pptx, pps, ppsx, pdf, xpf

在这种情况下,你建议我做什么?

PS:感谢您将来的答案。

1 个答案:

答案 0 :(得分:1)

如果你重构一些代码可能会有所帮助。考虑一下这个

attachments = params[:kayako_client_ticket][:attachment]

unless attachments.empty?
  attachments.inject([]) do |array, attachment|
    array << {filename: attachment.original_filename, data: File.new(attachment.tempfile).read}
  end
end