0

I have a page which i can add attachments. I wanted to add multiple files during browsing so i added the :multiple => true method on my input, so it looks like this during this input: f.has_many :attachments

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

For multiple attachments to be added / seen on the attachment panel i have this:

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?

In my model file, attachments are referenced like this:

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

And if I add multiple files , i get this error :

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

What do you recommend me to do in this situation?

PS: Thanks for your future answers.

Rails Coder.
  • 125
  • 1
  • 10

1 Answers1

1

It likely would help if you refactored your code some. Consider this

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
MilesStanfield
  • 4,571
  • 1
  • 21
  • 32
  • Hi there. It wasn't either kayako or carrierwave.It was more vast problem, so we created a function during our page for the number of attachments(size of file and total number of files) whilst the page was updating. Thanks anyway !! – Rails Coder. Feb 19 '16 at 13:10