Skip to content

Instantly share code, notes, and snippets.

@dacur
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save dacur/ea99b809e88af3b5d350 to your computer and use it in GitHub Desktop.

Select an option

Save dacur/ea99b809e88af3b5d350 to your computer and use it in GitHub Desktop.
Creating a DropZone for image uploads. Also sending a JavaScript global variable via the DropZone Headers to the controller. More Header info here: http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-headers
development:
access_key_id: "AKIAIRJ4D........"
secret_access_key: "qO/Xh8mOJ......."
simple_db_consistent_reads: false
production:
access_key_id: "AKIAIR.........."
secret_access_key: "qO/Xh8..........."
simple_db_consistent_reads: true
****** NOTHING TO CHANGE HERE, JUST COPY AND PASTE INTO AWS ***********
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
******* MAKE SURE TO USE YOUR OWN BUCKET NAME, NOT 'swaggr'! *********
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::swaggr/*"
}
]
}
// Call the function on Edit btn click; may not need 'headers'
// Make sure 'url' below is correct
function DropZone(){
alert(_selectedCatID);
$("#primaryImage").dropzone({
url: '/uploads/process',
maxFilesize: 1,
// paramName: "upload[image]",
headers: {'catID': _selectedCatID}, // sending the category id to the controller
addRemoveLinks: false,
success: function(file, data){
$('#primaryImage .dz-success-mark').css('opacity', '1');
$('#primaryImage .dz-progress').css('opacity', '0');
}
});
} //
# ****** CHANGE BUCKET NAME FROM 'foreverbridal' *********************
# SEE: http://docs.aws.amazon.com/AWSSdkDocsRuby/latest/DeveloperGuide/ruby-dg-samples.html
# **** YOU CANNOT HAVE A 'PROCESS' METHOD IN MAIN CONTROLLER!!! RENAME TO 'UPLOAD'
# FOR NO CONFLICTS! OTHERWISE YOU WILL GET ERRORS OR A WHITE/BLANK VIEW ********
# ALSO: you may not need '(stuff)'. If you get a 0 for 1 argument error, remove it.
def process(stuff)
# p stuff
file = params['file']
s3 = AWS::S3.new
# Upload a file.
key = File.basename(file.tempfile)
uploaded = s3.buckets['foreverbridal'].objects[key].write(:file => file.tempfile)
catID = request.headers['HTTP_CATID'] #Getting CatID via Header from DropZone call
image = uploaded.key # THIS IS THE IMAGE STRING/LOCATION/URL TO BE STORED IN DB
render json: { blah: image } # THIS IS WORKING! SENDS JSON TO SUCCESS CALL IN DROPZONE IN UPLOADS.JS
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment