使用 angular-file-upload, carrierwave 上传带参数的图片

upload image with parameters, using angular-file-upload, carrierwave

请帮我解决这个问题: 我正在使用此指令 angular-file-upload 上传带有参数的单张图片:

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()

这是创建操作:

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

这是请求:

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">}

我遇到了一个错误

undefined method `permit' for #<String:0xb40a6270>

谁能帮帮我

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

params[:recipe] = JSON.parse params[:recipe]