开发者

Best approach to multiple file uploads in Ruby on Rails

开发者 https://www.devze.com 2023-01-29 06:20 出处:网络
I have asked a similar question here with unsuccessful answers: Uploadify + Paperclip + Rails nested association before_save

I have asked a similar question here with unsuccessful answers: Uploadify + Paperclip + Rails nested association before_save

So i will reformulate my question:

What's the best approach in Rails to upload multiple files at once and associate them to an object that is not yet saved? (for example a model (girl) application form that is saved to the database when the create action is been complete (the save button is pressed).

Many ide开发者_StackOverflow中文版as come to my mind (save the object by ajax before he try to upload the images, add a token to images and then add the ID of the model after the model object is saved) but im pretty sure many people have done this and there's a common way or best approach to do it.

Thank you in advance! Martin.


I use this with one of my rails 3 apps:

= form_for :import_csv, :url => {:action => :import_csv}, :html => {:multipart => true} do |f|
  = f.file_field :csv
  = f.submit 'Upload CSV'

This creates a temporary file which can be retrieved with

CSV.open(params[:import_csv][:csv].tempfile.path)

I see no reason why this could not be extended to multiple uploads, and accessed at params[:import_csv][:whatever]

Note** the handling of tempfiles was changed slightly in rails 3.0.3, which is why the above code uses .tempfile.path which was not required in previous versions.


Over a year ago I was faced with a similar problem and could not find a ready solution therefore has made as follows:
1.Using SWFUpload upload images to an "store_image" action that stores, resizes, ..., and returns path to the thumbnail and the IDs of uploaded image.
2. Using JS put image IDs in hidden field(s) value. I used single field with value like "2312111:3231231:323212".
3. When create a "master" object, find the uploaded images by their IDs and establish their relation with the subject.

Also garbage collector removes unrelated images created over 3 days ago. The garbage collector runs by cron every day.

As for me, it is the most elegant solution.

__
Sorry for my bad English

0

精彩评论

暂无评论...
验证码 换一张
取 消