Status code 422 Unprocessable Entity - Paperclip, ImageMagick & Rails 3

蹲街弑〆低调 提交于 2019-12-13 12:21:48

问题


I am not sure what's causing this and would love some insight:

Started POST "/uploads.js" for 127.0.0.1 at 2011-02-21 09:14:36 -0500
  Processing by UploadsController#create as JS
  Parameters: {"name"=>"DSC_0561.JPG", "chunk"=>"0", "chunks"=>"1", "upload"=>{"stage_id"=>"16"}, "authenticity_token"=>"SfNGZm4lP56eG94OliSo2Kk9Tikg+X2pHaGoRLdbUmg=", "file"=>#<ActionDispatch::Http::UploadedFile:0x000001080f7ce8 @original_filename="DSC_0561.JPG", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"DSC_0561.JPG\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/+u/+u1vHpefHhSOuO43TGuqe++++TI/-Tmp-/RackMultipart20110221-9189-1ig5c62>>}

[paperclip] identify -format %wx%h '/var/folders/+u/+u1vHpefHhSOuO43TGuqe++++TI/-Tmp-/stream20110221-9189-1o6ij3e.JPG[0]' 2>/dev/null
[paperclip] convert '/var/folders/+u/+u1vHpefHhSOuO43TGuqe++++TI/-Tmp-/stream20110221-9189-1o6ij3e.JPG[0]' -resize "64x64" '/var/folders/+u/+u1vHpefHhSOuO43TGuqe++++TI/-Tmp-/stream20110221-9189-1o6ij3e20110221-9189-kssuro' 2>/dev/null
Completed 422 Unprocessable Entity in 1143ms (Views: 0.8ms | ActiveRecord: 1.5ms)

Not sure if Paperclip is throwing this error, or if it's ImageMagick or something else.

Edit1: So it seems that it was throwing that error because the file I was uploading had the extension as ".JPG", and once I changed it to ".jpg" it worked. But I am not sure how to fix that properly.


回答1:


Rails returning status code 422 means that validation failed so you are probably validating the upload's content type but it's always application/octet-stream because whatever you are uploading with isn't setting it properly.

You can use the mime-types gem to fix this but ideally you would fix the uploading code to correctly set the mime type.

mime_types = MIME::Types.type_for(params[:file])
file.content_type = mime_types.first.to_s if mime_types.first


来源:https://stackoverflow.com/questions/5067033/status-code-422-unprocessable-entity-paperclip-imagemagick-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!