marios
marios

Reputation: 261

ruby on rails:multiple upload and view

I am currently using paperclip to upload a single photo. When the user goes to the show view, it shows the photo and the info related to it. Now I want to update it to accept multiple photos so that all of the photos are shown on the show view. What do I need to use? (JavaScript and update paper-clip to take multiple uploads?) Or is there something else?

I have read http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/

Upvotes: 0

Views: 147

Answers (2)

Leo Correa
Leo Correa

Reputation: 19829

If you want to handle multi file upload I suggest you use this jquery library

https://github.com/blueimp/jQuery-File-Upload

Its very in depth and great for multi-file upload.

Also, this good example as to how to implement with paperclip.

Rails 3.1 + Paperclip + jQuery fileupload

Upvotes: 1

Yunwei.W
Yunwei.W

Reputation: 1599

Create a Photo class which will save photos. Like this

class Photo < ActiveRecord::Base

  belongs_to :your_class_having_photos

  #paperclip methods and validations ....
end

In your upload view:

<%=file_field "'photos'[]"%>
<%=file_field "'photos'[]"%>
<%=file_field "'photos'[]"%>

...

do it as many as you want by JS or hardcoding whatever. Then after submit, there will be a parameter called params[:photos] containing an array of multiple photos in it. Then you can do

params[:photos].each do |p|
   photo = @your_class_object.photos.build(p)
   photo.save
end

in your controller, or maybe in more nicer codes you want.

Upvotes: 0

Related Questions