Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Paperclip and Amazon S3 for zip file containing photos
    text
    copied!<p>I currently have a working photo uploader that creates Photo images using paperclip and aws-s3 gems. The loader can also dynamically add Photo upload fields so multiple files can be uploaded at once on a single submit. What I'd like to do is have the option of uploading a zip file with the expectation that the file contains Photos and have it run through my same process of creating thumbnails, medium size, and original images that the single photo file upload goes through. My model and controller is pretty straight forward with storing photos locally if on development, or on s3 if production, with just a little bit on fanciness with the view template:</p> <p><em>photo.rb</em></p> <pre><code>class Photo &lt; ActiveRecord::Base belongs_to :album if AppConfig['s3'] has_attached_file :data, :styles =&gt; { :thumb =&gt; "100x100&gt;", :medium =&gt; "500x500&gt;" }, :storage =&gt; :s3, :default_style =&gt; :original, :bucket =&gt; AppConfig['s3']['bucket_name'], :s3_credentials =&gt; { :access_key_id =&gt; AppConfig['s3']['access_id'], :secret_access_key =&gt; AppConfig['s3']['secret_key'] }, :s3_headers =&gt; { 'Cache-Control' =&gt; 'max-age=315576000', 'Expires' =&gt; 10.years.from_now.httpdate }, :path =&gt; "/:class/:id/:style/:filename" else has_attached_file :data, :styles =&gt; { :thumb =&gt; "100x100&gt;", :medium =&gt; "500x500&gt;" }, :storage =&gt; :filesystem, :default_style =&gt; :original end end </code></pre> <p>*photos_controller.rb*</p> <pre><code>class Admin::PhotosController &lt; Admin::AdminController def index @photos = Photo.all end def show @photo = Photo.find(params[:id]) end def new @photo = Photo.new end def create @photo = Photo.new(params[:photo]) if @photo.save flash[:notice] = "Successfully created photo." redirect_to [:admin, @photo] else render :action =&gt; 'new' end end def edit @photo = Photo.find(params[:id]) end def update @photo = Photo.find(params[:id]) album = @photo.album if @photo.update_attributes(params[:photo]) flash[:notice] = "Successfully updated photo." redirect_to [:admin, @photo] else redirect_to edit_admin_album_url(album) end end def destroy @photo = Photo.find(params[:id]) album = @photo.album @photo.destroy flash[:notice] = "Successfully destroyed photo." redirect_to edit_admin_album_url(album) end end </code></pre> <p>The interesting parts of the view are here:</p> <p>*_form.html.haml*</p> <pre><code> #photos - if @album.new_record? = render :partial =&gt; 'photo', :locals =&gt; { :form =&gt; f, :photo =&gt; @album.photos.build } - else - for photo in @album.photos .photo = link_to(image_tag(photo.data(:thumb)), photo.data(:medium), :class =&gt; 'photo_link') - f.fields_for @album.photos do |photo_field| / Viewable? / = photo_field.check_box :viewable %br = link_to "Delete", [:admin, photo], :confirm =&gt; 'Are you sure?', :method =&gt; :delete .float_clear = add_object_link("New Photo", f, @album.photos.build, "photo", "#photos") .row = submit_tag "Save", :disable_with =&gt; "Uploading please wait..." .float_clear </code></pre> <p>*_photo.html.haml*</p> <pre><code>.photo_form %p - form.fields_for :photos, photo, :child_index =&gt; (photo.new_record? ? "index_to_replace_with_js" : nil) do |photo_form| = photo_form.file_field :data = link_to_function "delete", "remove_field($(this), ('.photo_form'))" %br </code></pre> <p>Welcome all ideas or contributions! Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload