0

I have a web app: frontend in Angular and backend in Rails. In my app, I'm trying to add PDF file using Paperclip gem and send it to S3 storage.

Here is code for frontend(I found this code online but not sure whether I wrote onchange part correctly. Got it from here: Use angular js to submit file via rails paperclip ):

<input type="file" name="attachment" onchange="angular.element(this).scope().uploadFile(this)">

Here is my javascript method for uploadFile:

$scope.uploadFile = function(files) {
    var fd = new FormData();
    fd.append('user[attachment]', files[0]); // 'user[avatar]' is important, so that params[:user][:avatar] contains the file, as expected by Rails
    $http.put('/users.json', fd, { // The update method of the users controller
        withCredentials: true,
        headers: {
            'Content-Type': undefined,
            'X-CSRF-Token': $('meta[name=csrf-token]').attr('content')
        },
        transformRequest: angular.identity
    }).success(function(e) {
        console.log(e);
    }).error(function(e) {
        console.log(e);
    });
};

And this is how I configured this javascript file:

app.controller('PrescreeningBasicinfoCtrl', ['$scope', '$location', 'ApplicationModule', 'Restangular',
    function ($scope, $location, ApplicationModule, Restangular) {

Whenever I upload a file, it should trigger uploadFile. However, it does not get triggered. Instead i get an error saying

"Uncaught TypeError: angular.element(...).scope(...).uploadFile is not a function"

What am I doing wrong here?

Community
  • 1
  • 1
Jokingpsh
  • 25
  • 1
  • 9

0 Answers0