3

I am trying to submit form via ajax using remote:true option which contains input fields as well as file fields (need to upload images)

Here is my view

= form_tag("/campaigns/upload_form", method: 'post', format: :js, enctype: 'multipart/form-data', remote: true) do

which contains file field

= file_field :campaign, :fb_photo, class: 'form-control', id: 'fb_campaign_product_photo', title: 'Upload Image for your Product'

In controller action

def upload_form
    # do some stuff
    respond_to do |format|
     format.html
     format.js
    end
  end

When I submit form without file field it is processing and rendering as JS correctly

Started POST "/campaigns/upload_form" for 127.0.0.1 at 2016-03-21 18:06:51 +0530
Processing by CampaignsController#upload_form as JS

but when I am trying to submit file fields, it is processing as HTML and throwing template missing error as I haven't added view for that.

Editing this post to tell that: I have tried remotipart gem as suggested in below answer and it worked for me but still getting following error in browser console

jquery.js?body=1:9665 POST http://localhost:3000/campaigns/upload_form 500 (Internal Server Error)

and getting following error in development.log

 ActionView::MissingTemplate - Missing template campaigns/upload_form, application/upload_form with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
  * "/home/tudip/Documents/cpx/app/views"
  * "/home/tudip/.rvm/gems/ruby-2.2.1/gems/twitter-bootstrap-rails-3.2.0/app/views"
  * "/home/tudip/.rvm/gems/ruby-2.2.1/bundler/gems/devise-9568e28d663e/app/views"

Thanks in advance

Mahesh Khond
  • 1,297
  • 1
  • 14
  • 31

2 Answers2

8

remote: true does not work with multipart/file upload.

You can use remotipart gem for this.

https://github.com/JangoSteve/remotipart/blob/master/README.rdoc

dp7
  • 6,651
  • 1
  • 18
  • 37
  • I tried remotipart gem and it solved my problem but still I am getting missing template error in log file and also on browsers console. – Mahesh Khond Mar 21 '16 at 14:52
  • @MaheshwarKhond Do you have corresponding template i.e campaigns/upload_form.js.erb ? – dp7 Mar 21 '16 at 15:51
  • @ dkp Yes I have corresponding template upload_form.js.erb and solution is also working for me. Only problem is still Errors are logged in console as well as in development .log file – Mahesh Khond Mar 21 '16 at 16:11
  • I am able to upload image on production too but when I upload two images simultaneously It is not working – Mahesh Khond Apr 20 '16 at 11:18
-1

You're using form_tag instead of form_for. So, use file_field_tag instead of file_field.

jvillian
  • 19,953
  • 5
  • 31
  • 44
  • I have tried file_field_tag but still I am getting missing template error – Mahesh Khond Mar 21 '16 at 15:39
  • Please see @dkp comment asking whether you have upload_form.js.erb - I believe that's the correct next question. – jvillian Mar 21 '16 at 16:01
  • @ jvillian Yes I have corresponding template upload_form.js.erb and solution is also working for me. Only problem is still Errors are logged in console as well as in development .log file – Mahesh Khond Mar 21 '16 at 16:15