Does anyone have advice/examples on getting the jQuery File Upload plugin working with Rails using nested attributes?
My model "has_many" attachments and accept the necessary nested attributes. I would like to get this working with the jQuery File Upload but haven't been able to find any good examples to get me started.
Has anyone achieved something like this and able to give some direction?
Thanks.
I have coped with this problem and made a demo app to show how to do this.
In short I have two models: item and upload.
item.rb:
upload.rb:
I added
uploads_attributes
to item controller.Now you can add jquery-file-upload form to your view, but there is one problem: it sends each photo in separate requests. So there is my jquery-file-upload initializer, which uploads all photos in one request (creating item model) and then redirecting to the root of your app (you need to use item form):
Take a look at the 7 minute mark of this video. http://ror-e.com/info/videos/3
It describes a way to upload images "product has_many images" You should look at the ror_ecommerce source and in admin/merchandise/images/products you can look at the code.
https://github.com/drhenner/ror_ecommerce
The JS is in application.js and the application_helper.rb
I spent a long time getting it to work in a project of mine. Here's a link to a giant SO post I made on a different question. It may help you.
Rails 3 + JQuery-File-Upload + Nested Model
I've succesfuly set it up to work when editing a model with nested attachments.
The same is not possible when creating a new model - at least not currently due to paperclip.
You have to set up some jQuery File Upload options which I found here.
You should call
fileUploadUI()
on the whole form, and set file input wrapper element asdropZone
. You should also seturl
,fieldName
andformData
accordingly.Here's how my JS looks like (simplified):