Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

Rails 3 Image upload form submitting with http instead of AJAX

I am working on a form to submit it by AJAX instead of http. This is the form :

    <%= form_for(:image, :remote => true, :url => {:controller=> 'questions',:action => 'upload'},:multipart => true) do |f| %>

    <%= f.file_field :image, :onchange => "$(this).parents('form').submit();"  %>

    <% end %>

I have set the :remote => true option above and submitting the form with an onchange event . I have the following code in controller :

def upload

     if request.xhr?
        @image = Image.new(params[:image])
        @image.save

            respond_to do |format|
                format.js { render :layout=>false }
           end
     else
           render :text => 'Request Wasnt AJAX'
     end
end

My action renders the text everytime , the request does not seem to be AJAX style despite the remote tag being set (it appears correctly even in the final HTML). I can't figure out where I am going wrong with this . I have tested it in the latest browser version of FF and Chrome , so I don't think it's a browser issue. Any ideas ?

Update : I did some more debugging attempts . The issue is with the file field , if I replace the file field with text field , the request is AJAX (everything else remaining same) . But with a file field it always sends a non AJAX request.

Note : Overall objective is to upload an image via AJAX request, with the response rendering nothing, no HTML, no redirection, no reload of the page.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Got it to work by installing the Remotipart gem . To upload image files using ajax form submission , this is the only way . Find the git here :https://github.com/JangoSteve/remotipart


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...