3

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 ideas 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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

But isn't insecure to just keep the ids as hidden on the page? Wouldn't be the user be able to write other ids?
I use this solution in the administration panel so security issues are not critical. To increase security you can use GUIDs as identifiers of images, limit the number of images and check the number of colons in the field using regular expressions. You can also check if the relationship of the "new" image already set, and if so throw an error. There are many other opportunities to increase security, for example you may set ownership of images, or use sessions to store ids of uploaded images (but as for me, the last solution is very ugly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.