即使照片上传,Carrierwave也会返回406不可接受

发布于 2024-11-29 18:33:09 字数 2589 浏览 0 评论 0原文

我正在使用 Carrierwave 通过一个简单的控制器将照片上传到我的 Rails 应用程序:

  # POST /photos
  # POST /photos.xml
  def create
    @photo = Photo.new(params[:photo])

    respond_to do |format|
      if @photo.save
        format.html { redirect_to(@photo, :notice => 'Photo was successfully created.') }
        format.xml  { render :xml => @photo, :status => :created, :location => @photo }
        format.json { render :json => @photo, :status => :created }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
        format.json { render :json => @photo.errors, :status => :unprocessable_entity }
      end
    end
  end

我通过 Ajax 发送文件,尽管上传实际上已正确保存和处理,Rails 仍然返回一个神秘的 Completed 406 Not Acceptable< /code>:

Started POST "/photos" for 127.0.0.1 at 2011-08-17 14:20:01 +0300
  Processing by PhotosController#create as JS
  Parameters: {"Filename"=>"IMG_5758.JPG", "folder"=>"/apartments/4e4ba2e735be35f40a00000b/", "_http_accept"=>"application/javascript", "fileext"=>"*.png;*.jpg;*.gif", "_dirot_session"=>"BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlOTYxNGM2MDk3NmU0YjRjN2U3NjMzNTA1YmQwMDg5MWJJIgx1c2VyX2lkBjsARm86E0JTT046Ok9iamVjdElkBjoKQGRhdGFbEWlSaQH0aQHtaQGUaTppAb5pOmkSaQGAaQBpAGkGSSIQX2NzcmZfdG9rZW4GOwBGSSIxb2RDcm9OMXRkYis3MXZpQ1ZtN1ZkZE54TmhTckFzalVBUmFTQWxxeU4yQT0GOwBG--2b5bc2b162dc2a1423750cbae32d5238d5c45064", "authenticity_token"=>"odCroN1tdb 71viCVm7VddNxNhSrAsjUARaSAlqyN2A=", "photo"=>{"apartment_id"=>"4e4ba2e735be35f40a00000b", "image"=>#<ActionDispatch::Http::UploadedFile:0x000001033fe810 @original_filename="IMG_5758.JPG", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"IMG_5758.JPG\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/qI/qIWmC9LgFB0Dk8E4z1xjAE+++TI/-Tmp-/RackMultipart20110817-62474-4akysm>>}, "Upload"=>"Submit Query"}
Creating scope :near. Overwriting existing method Apartment.near.
MONGODB dirot_development['apartments'].find({:_id=>BSON::ObjectId('4e4ba2e735be35f40a00000b')})
MONGODB dirot_development['photos'].insert([{"apartment_id"=>BSON::ObjectId('4e4ba2e735be35f40a00000b'), "_id"=>BSON::ObjectId('4e4ba3ea35be35f40a00000f'), "updated_at"=>2011-08-17 11:20:10 UTC, "image_filename"=>"IMG_5758.JPG", "created_at"=>2011-08-17 11:20:10 UTC}])
Completed 406 Not Acceptable in 9125ms

你知道为什么会发生这种情况吗?

I am using Carrierwave to upload photos to my rails app using a simple controller:

  # POST /photos
  # POST /photos.xml
  def create
    @photo = Photo.new(params[:photo])

    respond_to do |format|
      if @photo.save
        format.html { redirect_to(@photo, :notice => 'Photo was successfully created.') }
        format.xml  { render :xml => @photo, :status => :created, :location => @photo }
        format.json { render :json => @photo, :status => :created }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
        format.json { render :json => @photo.errors, :status => :unprocessable_entity }
      end
    end
  end

I'm sending the files via Ajax, and despite the upload actually being saved and processed correctly, Rails is still returning a mysterious Completed 406 Not Acceptable:

Started POST "/photos" for 127.0.0.1 at 2011-08-17 14:20:01 +0300
  Processing by PhotosController#create as JS
  Parameters: {"Filename"=>"IMG_5758.JPG", "folder"=>"/apartments/4e4ba2e735be35f40a00000b/", "_http_accept"=>"application/javascript", "fileext"=>"*.png;*.jpg;*.gif", "_dirot_session"=>"BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlOTYxNGM2MDk3NmU0YjRjN2U3NjMzNTA1YmQwMDg5MWJJIgx1c2VyX2lkBjsARm86E0JTT046Ok9iamVjdElkBjoKQGRhdGFbEWlSaQH0aQHtaQGUaTppAb5pOmkSaQGAaQBpAGkGSSIQX2NzcmZfdG9rZW4GOwBGSSIxb2RDcm9OMXRkYis3MXZpQ1ZtN1ZkZE54TmhTckFzalVBUmFTQWxxeU4yQT0GOwBG--2b5bc2b162dc2a1423750cbae32d5238d5c45064", "authenticity_token"=>"odCroN1tdb 71viCVm7VddNxNhSrAsjUARaSAlqyN2A=", "photo"=>{"apartment_id"=>"4e4ba2e735be35f40a00000b", "image"=>#<ActionDispatch::Http::UploadedFile:0x000001033fe810 @original_filename="IMG_5758.JPG", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"IMG_5758.JPG\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/qI/qIWmC9LgFB0Dk8E4z1xjAE+++TI/-Tmp-/RackMultipart20110817-62474-4akysm>>}, "Upload"=>"Submit Query"}
Creating scope :near. Overwriting existing method Apartment.near.
MONGODB dirot_development['apartments'].find({:_id=>BSON::ObjectId('4e4ba2e735be35f40a00000b')})
MONGODB dirot_development['photos'].insert([{"apartment_id"=>BSON::ObjectId('4e4ba2e735be35f40a00000b'), "_id"=>BSON::ObjectId('4e4ba3ea35be35f40a00000f'), "updated_at"=>2011-08-17 11:20:10 UTC, "image_filename"=>"IMG_5758.JPG", "created_at"=>2011-08-17 11:20:10 UTC}])
Completed 406 Not Acceptable in 9125ms

Do you know why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-12-06 18:33:09

您的控制器正在将您的请求作为“JS”处理。但是,您的控制器操作仅响应 html、xml 和 json。添加 format.js 行和相关响应。

Your controller is processing your request as "JS". However, your controller action only responds to html, xml and json. Add lines for format.js and the relevant responses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文