Skip to content

Instantly share code, notes, and snippets.

@mivanek
Created August 21, 2013 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mivanek/f37fc734a30106760ff5 to your computer and use it in GitHub Desktop.
Save mivanek/f37fc734a30106760ff5 to your computer and use it in GitHub Desktop.
jquery-file-upload
<%= form_for [@project, attachment], :html => {:id=>"fileupload", :class => type } do |f| %>
<%= hidden_field_tag 'attachment_type', type %>
<%= hidden_field_tag 'project_id', project_id %>
<span class="file-upload button">
<span>Attach a File</span>
<%= f.file_field :path %>
</span>
<table id="results"><tbody class="files"></tbody></table>
<% end %>
$(function () {
// Initialize the jQuery File Upload widget:
$('#fileupload.<%= type %>').fileupload({
autoUpload: true,
dataType: 'json'
}).bind('fileuploadalways', function (e, data) {
window.showQuickForm();
}).bind('fileuploadcompleted', function (e, data) {
// the current variable points to the hidden field that is used in
// the issues form to send relevant data to the issues create action
var current = $("<%= field_id %> input#attach_list").attr("value");
$.each(data.result, function (index, file) {
// if the field is empty, set the value of the field to the uploaded's file name
if (current =="") {
current = file.id;
} else {
// if it's not empty, append the file name to the already existing values
current += "," + file.id;
}
});
$("<%= field_id %> input#attach_list").attr("value", current);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment