Skip to content
guptapriyank edited this page Nov 14, 2014 · 17 revisions

Angular Code :

So here I am using Rails 4 with paperclip for attachments.

Showing below is the upload code for controller in Angularjs

for (var i = 0; i < $files.length; i++) {
    var file = $files[i];
    $scope.upload = $upload.upload({
      url: 'server/upload/url',
      method: 'POST',
      data: mydata,//it is the data that's need to be sent along with image
      file: file,
      fileFormDataName: 'myEntity[image]',//myEntity is the name of the entity in which image is saved and image is the name of the attachment
      formDataAppender: function(fd, key, val) {
        if (angular.isArray(val)) {
            angular.forEach(val, function(v) {
                fd.append('myEntity['+key+']', v);
            });
        } else {
            fd.append('myEntity['+key+']', val);
        }
      }
  }

so, here my aim is to save data in a model with the attachment fields in it as per paperclip. Let us consider I want to save user with his image and the respective model is defined as

user_id : integer
name : string
// other fields related to attachment are being merged by using paperclip migration

Clone this wiki locally