jQuery文件上传ActionController :: UnknownFormat

时间:2013-12-17 18:48:02

标签: jquery ruby-on-rails file-upload carrierwave

我正在尝试使用jquery-fileupload-rails

上传多张图片

这是我的javascript:

<script type="text/javascript" charset="utf-8">
  $(function () {
      // Initialize the jQuery File Upload widget:
      $('#fileupload').fileupload({
        paramName: 'album_picture[picture]'
      });
      // 
      // Load existing files:
      $.getJSON($('#fileupload').prop('action'), function (files) {
        var fu = $('#fileupload').data('blueimpFileupload'), 
          template;
        fu._adjustMaxNumberOfFiles(-files.length);
        console.log(files);
        template = fu._renderDownload(files)
          .appendTo($('#fileupload .files'));
        // Force reflow:
        fu._reflow = fu._transition && template.length &&
          template[0].offsetWidth;
        template.addClass('in');
        $('#loading').remove();
      });

  });
</script>

以下是请求标头:

POST /album_pictures HTTP/1.1
Host: 127.0.0.1:3000
Connection: keep-alive
Content-Length: 98937
Accept: undefined
Origin: http://127.0.0.1:3000
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryFhpKR7ztl9s9QWAZ
Referer: http://127.0.0.1:3000/album_pictures/new/3
Accept-Encoding: gzip,deflate,sdch
Accept-Language: he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4

这是请求参数:

{"utf8"=>"✓", "authenticity_token"=>"xXkbJNMVVNy3SocUJKKHHts0RO8PEP8aGqw8C+dlaKw=", "album_picture"=>{"album_id"=>"3", "picture"=>#<ActionDispatch::Http::UploadedFile:0x007fac660e8c28 @tempfile=#<Tempfile:/var/folders/_7/jc0746b936q9b3q3lsf7xj7m0000gn/T/RackMultipart20131217-71351-1csiua>, @original_filename="gall1_big5.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"album_picture[picture]\"; filename=\"gall1_big5.jpg\"\r\nContent-Type: image/jpeg\r\n">}}

我一直得到以下内容:

ActionController::UnknownFormat (ActionController::UnknownFormat):
  app/controllers/album_pictures_controller.rb:49:in `create'

 respond_to do |format|

这是我的控制器代码:

def create
    @album_picture = AlbumPicture.new(album_picture_params)

    respond_to do |format|
      if @album_picture.save
        format.html {
          render :json => [@album_picture.to_jq_upload].to_json,
          :content_type => 'text/html',
          :layout => false
        }
        format.json { render json: {files: [@album_picture.to_jq_upload]}, status: :created, location: @album_picture }
      else
        format.html { render action: "new" }
        format.json { render json: @album_picture.errors, status: :unprocessable_entity }
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

问题出在您的控制器中,您的respond_to块格式不正确。如果您想要更具体的答案,请发布该代码。