I've been searching for some examples, but have come up short:
I'm trying to implement JQuery-File-Upload on a project I'm working on, but am getting lost as to how to get it to work with nested attributes.
Quick overview:
2 Models:
Comment [has_many :attachments]
Attachment [belongs_to :comment]
Comment accepts_nested_attributes_for :attachments
. Also - I'm using Dragonfly.
I've reviewed the Rails 3 guides on the JQuery-File-Upload site, but they assume it's a singular model, so it's all built around a form. Does anyone have any examples of their implementation or is there an existing tutorial that I haven't yet stumbled across?
I'm sure someone has had a similar issue... is JQuery-File-Upload to appropriate tool or should I look at something else?
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):
I just wanted to throw my answer in here as well as Stone's. I spent nearly two solid days getting this to work (Stone was right, it was a PITA!), so hopefully my solution will help someone. I did it just a touch different than Stone.
My app has
Features
(a comic, puzzle, text-column, etc) andFeatureAssets
(individual comic panels/color versions, question & answer files for a specific crossword, etc). SinceFeatureAssets
are solely related to oneFeature
, I nested the models (as you'll see in my upload form).The biggest problem for me was realizing that my
params[:feature_asset]
that was being sent to the server was actually an array of my uploader'dfile
objects, instead of just the one I was used to working with. After a bit of fiddling with iterating through each file and creating a FeatureAsset from it, it worked like a charm!Hopefully I'll translate this clearly. I'd rather provide a bit too much information than not enough. A little extra context never hurts when you're interpreting someone else's code.
feature.rb
feature_asset.rb
feature_assets_controller.rb
And not that it probably helps that much, but my feature_asset_uploader.rb is below. It's pretty stripped down.
features _form.html.erb (similar to Stone's, but not quite)
It doesn't have error handling or any of the niceties it should have, but that's the barebones version of it.
Hopefully that helps someone out there. Feel free to ask me if you have any questions!
Kyle
I have a similar setup running with Carrierwave. Here's what I have. I'm using Images as a nested resource for Projects.
Project.rb:
Image.rb:
Images_controller.rb:
Be aware, this was a *!@^%! to get working.
UPDATE: projects/_form.html.erb