【问题标题】:How I can associate my user id in angular-file-upload with rails如何将 angular-file-upload 中的用户 ID 与 rails 相关联
【发布时间】:2015-06-15 17:22:26
【问题描述】:

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

这将是模型的结构

用户有很多照片

示例代码:

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

HTML 代码:

<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

【问题讨论】:

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


    【解决方案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])
    

    【讨论】:

    • @WesFoster 我编辑了我的答案,抱歉没有解释。
    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2012-06-25
    • 2013-09-09
    • 2017-10-20
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多