Skip to content

Instantly share code, notes, and snippets.

@eddiefisher
Created March 22, 2015 15:16
Show Gist options
  • Save eddiefisher/1e92a4e35f6fd8c60c29 to your computer and use it in GitHub Desktop.
Save eddiefisher/1e92a4e35f6fd8c60c29 to your computer and use it in GitHub Desktop.
refile multiply file upload
= semantic_nested_form_for @gallery, multipart: true do |form|
= form.input :file_multiple, as: :file, input_html: { multiple: true }
= form.actions do
= form.action :submit
class GalleriesController < SpotControllerBase
# some code ...
def update
if @gallery.update_attributes(params[:gallery])
create_multiple_files(:gallery, @gallery)
redirect_to gallery_path(@gallery)
else
render action: "edit"
end
end
def create
@gallery = Gallery.create!(params[:gallery])
create_multiple_files(:gallery, @gallery)
redirect_to gallery_path(@gallery)
end
private
def create_multiple_files(parent, record)
multiple = params[parent][:file_multiple]
multiple.each { |file| record.photos.create! file: file }
end
end
class Photo < ActiveRecord::Base
belongs_to :gallery
attachment :file
before_validation :generate_file_filename
private
def generate_file_filename
extension = self.file_content_type == nil ? '' : Rack::Mime::MIME_TYPES.invert[self.file_content_type]
filename = self.gallery.nil? || self.gallery.title.blank? ? 'photo' : self.gallery.title.parameterize
self.file_filename = "#{filename}#{extension}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment