I am trying to upload an image in RAILS 3 using AJAX form submission , this is my code : <%= form_for(:image, :remote => true, :url => {:controller=> ''questions'',:action => ''upload''}, :html => {:id=>''qimage_upload''}) 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'' endend If I change the field to text in the form , the request is AJAX(everything else remaining same) but with an image field the request is always AJAX . I have included ''jquery'' and ''jquery_ujs'' in my application.js file as well. 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 ? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/IoBk58iurt0J. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-May-08 09:58 UTC
Re: Rails 3 : Upload image file using AJAX form submission
On Wednesday, May 8, 2013 10:40:32 AM UTC+1, Varun Jain wrote:> > I am trying to upload an image in RAILS 3 using AJAX form submission , > this is my code : > > You can''t upload files over ajax (at least not without using the HTML5File apis, which you may not be able to rely on). The usual trick is to do the upload in a hidden iframe so that it doesn''t block the ''main'' page. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/4Gw_yZHJvdMJ. For more options, visit https://groups.google.com/groups/opt_out.