首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传带有参数的图像,使用角文件-上传,载波

上传带有参数的图像,使用角文件-上传,载波
EN

Stack Overflow用户
提问于 2015-12-06 20:13:17
回答 1查看 539关注 0票数 0

请帮助我完成以下工作:我使用这个指令角文件上传上传带有参数的单个图像:

代码语言:javascript
复制
uploader = $scope.uploader = new FileUploader(
      url: '/recipes',
      alias:  'cover',
      removeAfterUpload:  true,
      #transformRequest: angular.identity,
      headers: {'X-CSRF-TOKEN': csrf_token,'accept': 'application/json'},
      withCredentials: true
    )


uploader.onBeforeUploadItem = (item)->
          #data = angular.toJSON($scope.recipe)
          item.formData.push("recipe": angular.toJson($scope.recipe))
          #item.upload()
          console.info('uploader', $scope.uploader);
          uploader.uploadAll()

这是创建操作:

代码语言:javascript
复制
def create

        @recipe = current_user.recipes.new(params.require(:recipe).permit(:name,:instructions))
        @recipe.user_id = current_user.id
        @recipe.save
        render 'show', status: 201
      end

这是请求:

代码语言:javascript
复制
Started POST "/recipes" for 127.0.0.1 at 2015-12-06 20:29:20 +0100
Processing by RecipesController#create as JSON
  Parameters: {"recipe"=>"{\"name\":\"cnfsl*k\",\"instructions\":\"mcdmùdsd\",\"ingredients\":[{\"id\":4,\"title\":\"sucre\"}]}", "cover"=>#<ActionDispatch::Http::UploadedFile:0xb42de9e8 @tempfile=#<Tempfile:/tmp/RackMultipart20151206-5852-txz5ls.jpg>, @original_filename="6.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"cover\"; filename=\"6.jpg\"\r\nContent-Type: image/jpeg\r\n">}

我有个错误

代码语言:javascript
复制
undefined method `permit' for #<String:0xb40a6270>

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-06 20:21:31

您的params[:recipe]不是hash,而是json。您需要先解析它。尝试将这一行添加为create操作中的第一行:

代码语言:javascript
复制
params[:recipe] = JSON.parse params[:recipe]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34122156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档