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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…