Skip to content
guptapriyank edited this page Dec 15, 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

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

so we can change the AngularJs controller code as

for (var i = 0; i < $files.length; i++) {
    var file = $files[i];
    $scope.upload = $upload.upload({
      url: 'server/upload/url',
      method: 'POST',
      data: {name : $scope.name},
      file: file,
      fileFormDataName: 'user[image]',
      formDataAppender: function(fd, key, val) {
        if (angular.isArray(val)) {
            angular.forEach(val, function(v) {
                fd.append('user['+key+']', v);
            });
        } else {
            fd.append('user['+key+']', val);
        }
      }
  }

Here I save the attachment with name image Lets have a look on our controller code for Rails :

class UserController < ApplicationController
  def saveUser
    @user = Hotel.new(user_params)
    if @user.save
      render json: {success: true}
    else
      render json: @user.errors
    end  
  end  

  private 
    def user_params
      params.require(:user).permit(:name, :image)
    end 
end

Thats it, We are done.

Clone this wiki locally