开发者

Rails 3 with Uploadify & Paper_Clip

Anyone here haveRails 3 with Uploadify & Paper_Clip experience?

I'd like to allow the user to upload photos:

Here is my view:

<input id="fileInput" name="fileInput" type="file" />
    <script type="text/javascript">
        $(document).ready(function() {

            $('#fileInput').uploadify({
                uploader : '/uploadify/uploadify.swf',
                fileDataName : 'photo[file]',
                script : '/photos/upload',
                cancelImg : '/uploadify/cancel.png',
                auto : true,
                scriptData : {
                    '_http_accept': 'application/javascript',
                    '_method': 'put',
                    "<%= key = Rails.application.config.session_options[:key] %>" : "<%= cookies[key] %>",
                    "<%= request_forgery_protection_token %>" : "<%= form_authenticity_token %>"

                }
            });
        });
    </script>

My Controller:

  def upload
    @photo = Photo.create(params[:file])

    respond_to do |format|
      format.json
    end
  end

My Model: #Paperclip for photo has_attached_file :photo, . . .

PROBLEM... is when I use uploadify to render, if errors, In the logs it shows:

Started POST "/photos/upload" for 127.0.0.1 at Sun Oct 24 12:16:38 -0700 2010
  Processing by PhotosController#upload as HTML
  Parameters: {"photo"=>{"file"=>#<File:/var/folders/lF/lF0Ne5vGFj44kV54W3zBdU+++TI/-Tmp-/RackMultipart20101024-34605-sqvg1m-0>}, "_http_accept"=>"application/javascript", "Filename"=>"whiteboard_2.jpg", "folder"=>"/projects/3/photo_albums/", "authenticity_token"=>"l9TqKh1Q7/8riUjuPMj7QtiJwNNxvBKfENnTx/ijP3E=", "Upload"=>"Submit Query", "_cline_session"=>"BAh7CCIQX2NzcmZfdG9rZW4iMWw5VHFLaDFRNy84cmlVanVQTWo3UXRpSndOTnh2QktmRU5uVHgvaWpQM0U9Ig9zZXNzaW9uX2lkIiVmYjYyYmM5YjU0YTBkMWMwNWZmMDQ3NmE1NzU1YTE1Y开发者_Python百科SIZd2FyZGVuLnVzZXIudXNlci5rZXlbByIJVXNlcmkG--f5b0cb3a6b775b2cc15eb30b20aa3c3f558b0412"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
  SQL (0.4ms)  INSERT INTO "photos" ("created_at", "description", "photo_content_type", "photo_file_name", "photo_file_size", "photo_updated_at", "title", "updated_at", "user_id") VALUES ('2010-10-24 19:16:39.042342', NULL, NULL, NULL, NULL, NULL, NULL, '2010-10-24 19:16:39.042342', NULL)
[paperclip] Saving attachments.
Completed 406 Not Acceptable in 50ms

Any ideas why Rails isn't getting the uploaded file?

Thanks


I guess this is what you are looking for: http://github.com/websymphony/Rails3-Paperclip-Uploadify


Because you can't upload images over a normal AJAX request (see uploading a file via ajax with php), paperclip gem keeps the request as a standard 'text/html' and sends the file up.

If you had a response in your controller for HTML, you would see it send back the HTML, which is what your browser requested. Check the Firefox LiveHTTPHeaders to compare a normal :remote call to one with a paperclip attachment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜