Created
March 22, 2015 15:16
-
-
Save eddiefisher/1e92a4e35f6fd8c60c29 to your computer and use it in GitHub Desktop.
refile multiply file upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Gallery < ActiveRecord::Base | |
has_many :photos, dependent: :destroy | |
attr_accessor :file_multiple | |
accepts_nested_attributes_for :photos, allow_destroy: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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