最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remote: true option is not working with file field in rails 4 - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Mar 21, 2016 at 16:26 Mahesh Khond asked Mar 21, 2016 at 13:35 Mahesh KhondMahesh Khond 1,3071 gold badge14 silver badges31 bronze badges 1
  • stackoverflow./questions/7376304/… – Rajarshi Das Commented Mar 21, 2016 at 13:55
Add a ment  | 

2 Answers 2

Reset to default 8

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

You can use remotipart gem for this.

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

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

发布评论

评论列表(0)

  1. 暂无评论