如何将angular-file-upload中的用户ID与rails相关联

时间:2015-04-10 05:08:32

标签: ruby-on-rails angularjs file-upload

我正在使用带有Rails的angular-file-upload(https://github.com/nervgh/angular-file-upload)..我想在上传照片时关联外键的用户ID。

这将是模型的结构

用户有很多照片

示例代码:

var uploader = $scope.uploader = new FileUploader({
    url: 'http://localhost:3000/photos/'
});

HTML code:

<input type="file" nv-file-select="" uploader="uploader" multiple  /><br/>

<button type="button" class="btn btn-success btn-xs" ng-click="item.upload()" ng-disabled="item.isReady || item.isUploading || item.isSuccess">
                        <span class="glyphicon glyphicon-upload"></span> Upload


<button type="button" class="btn btn-success btn-s" ng-click="uploader.uploadAll()" ng-disabled="!uploader.getNotUploadedItems().length">
            <span class="glyphicon glyphicon-upload"></span> Upload all

1 个答案:

答案 0 :(得分:1)

你想要做这样的事情: assuming the user's id is in $scope.user.id

var uploader = $scope.uploader = new FileUploader({
    url: 'http://localhost:3000/photos/',
    formData: [
      user_id: $scope.user.id
    ]
});

这会在具有用户ID的请求中添加新参数 然后在rails控制器中你可以做类似的事情:

@photo = Photo.create(photo: params[:file],user_id: params[:user_id])