skip to Main Content

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

2

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search