简体   繁体   中英

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

plz help me with this: i'm using this directive angular-file-upload to upload single image with parameters:

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

this is the create action:

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

this is the request:

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

i got an error

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

can anyone help me please

Your params[:recipe] is not a hash but a json . You need to parse it first. Try to add this line as the first line in your create action:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM