Forum: Ruby on Rails multiple file upload form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Scott N. (Guest)
on 2005-12-10 19:00
I am trying to modify the image upload example in the Agile book to
handle mtltiple files.

I have been playing with it for a while now, but no luck.

Do I have to create a loop, and do each file seperate, or can I just
save an array of files?

How can I set up my form and controller to handle this? Will I have to
modify the model?
Nick S. (Guest)
on 2005-12-10 23:05
Hi,

Scott I have stuggled with the same problem. Ultimately I have resorted
to file_column, a plugin that can handle multiple files. You can find it
at http://www.kanthak.net/opensource/file_column/ .

To handle multiple files, you should put something like the following in
your model:

class Entry < ActiveRecord::Base
   file_column :image1
   file_column :image2
   file_column :image3
end

Kind regards,

Nick

scottnj wrote:
> I am trying to modify the image upload example in the Agile book to
> handle mtltiple files.
>
> I have been playing with it for a while now, but no luck.
>
> Do I have to create a loop, and do each file seperate, or can I just
> save an array of files?
>
> How can I set up my form and controller to handle this? Will I have to
> modify the model?
g.arrowsmith (Guest)
on 2005-12-11 00:24
(Received via mailing list)
Nick S. <nick.snels@...> writes:

> class Entry < ActiveRecord::Base
> > I am trying to modify the image upload example in the Agile book to
> > handle mtltiple files.
> >
> > I have been playing with it for a while now, but no luck.
> >
> > Do I have to create a loop, and do each file seperate, or can I just
> > save an array of files?
> >
> > How can I set up my form and controller to handle this? Will I have to
> > modify the model?
>

I used the following js to create a dynamic array of files and then loop
through
the array. (stickman's multiple upload javascript)
http://snipurl.com/ko7u

then use something like:

@files = []
params[:file].each {|k,v| @files[k.to_i] = v if v.size>0}

for file in @files
  do stuff
end

Works a treat. Note- I found that for some reason an empty file element
seemed
to be created which screwed everything up so added v.size>0.

Hope this is of interest.

Graham.
Scott N. (Guest)
on 2005-12-11 00:34
g.arrowsmith wrote:
> I used the following js to create a dynamic array of files and then loop
> through
> the array. (stickman's multiple upload javascript)
> http://snipurl.com/ko7u

I saw this and am considering it, but I need something to work if
javascript is disabled.
This topic is locked and can not be replied to.