Вот, держи, должно натолкнуть на мысли.
requesting_controller
def create
@requesting = Requesting.create(requesting_params)
dir = upload_dir
FileUtils.mkdir_p(dir) unless File.directory?(dir)
counter = 0
params[:uploadedFiles].each do |file_element|
file_element.original_filename = format('%02d', counter) + '__' + file_element.original_filename
file = nil
File.open(Rails.root.join(dir, file_element.original_filename), 'wb') do |_file|
_file.write(file_element.read)
file = _file
end
proposal_file = ProposalFile.new
proposal_file.path = file_element
proposal_file.path = File.open(file)
proposal_file.requesting = @requesting
proposal_file.save!
counter += 1
end
end
модель
class Requesting < ActiveRecord::Base
has_many :proposal_files
belongs_to :user
end
вьюха
<div id = 'submit_handler'><script id="new-file-input" type="text/x-tmpl">
<div class="file_input_wrapper"><%= f.file_field :file, multiple: true, name: 'uploadedFiles[]', class: 'btn btn-link', width: 100 %></div></script></div>
<br>
upload.js
$(function() {
function addNewField() {
$('#submit_handler').before(tmpl('new-file-input'));
}
function deleteButton(element) {
$(element).after(tmpl('delete-button'));
}
addNewField();
$(document).on('change', '#requesting_file', function() {
var field = $(this);
field.removeAttr('id').prop('disabled', true).addClass('added-file');
deleteButton(field);
addNewField();
});
$(document).on('click', '.delete-file', function() {
$(this).closest('.file_input_wrapper').remove();
});
$('#new_upload').on('submit', function() {
$('.added-file').prop('disabled', false);
});
});