Reputation: 1309
I'm trying to create a form that has 3 models (1 for the actual form, 1 for other entries and another for the uploaded archives). I want to use jQuery File Upload gem to upload my PDFs to the server, but I'm also using the ActiveAdmin gem that handles this forms.
How can I create a multi-upload file input and add that through my Files model inside ActiveAdmin?
I should have:
Upvotes: 1
Views: 4070
Reputation: 774
For a form with multiple uploads you can try this:
# active admin
form do |f|
f.inputs "ModelName" do
f.input :name
end
f.has_many :attachments do |ff|
ff.input :path
end
end
# your_model.rb
attr_accessible :attachments_attributes
has_many :attachments
# your_model.rb (add after relations)
accepts_nested_attributes_for :attachments, :allow_destroy => true
see also: accept nested attributes for has_many relationship
Use of rails validators will hinder the form from being saved if not passed.
# attachment.rb
validates :check_size
validates :check_if_pdf
def check_size
errors.add :path, "Size is NOT ok" if self.size < XXX
end
def check_if_pdf
errors.add :path, "File is NOT pdf" unless self.path.to_s.split('.').last == 'pdf'
end
Not sure about Paperclip...Carrierwave is awesome as well and if you are open to that gem you can try this:
# attachment.rb
mount_uploader :path, MyUploader
# app/uploaders/my_uploader.rb
class MyUploader < CarrierWave::Uploader::Base
storage :file # For local storage
#storage :fog # If using S3
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def filename
@name ||= "#{File.basename(original_filename, '.*')}" if original_filename.present?
end
end
Carrierwave has an extension_white_list method that you can try to validate that its a PDF document
# uploaders/my_uploader.rb
def extension_white_list
%w(pdf jpg jpeg gif png csv )
end
Upvotes: 2