remote:true选项不适用于rails 4中的文件字段 [英] remote: true option is not working with file field in rails 4
问题描述
我正在尝试使用remote:true选项通过Ajax提交表单,该选项包含输入字段以及文件字段(需要上传图片)
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)
这是我的观点
= form_tag("/campaigns/upload_form", method: 'post', format: :js, enctype: 'multipart/form-data', remote: true) do
其中包含文件字段
= file_field :campaign, :fb_photo, class: 'form-control', id: 'fb_campaign_product_photo', title: 'Upload Image for your Product'
在控制器动作中
def upload_form
# do some stuff
respond_to do |format|
format.html
format.js
end
end
当我提交不带文件字段的表单时,它正在正确处理并呈现为JS
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
但是当我尝试提交文件字段时,它正在作为HTML处理并抛出模板丢失错误,因为我没有为此添加视图.
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.
编辑此帖子以告知: 我尝试了以下答案中建议的remotipart gem,它对我有用,但在浏览器控制台中仍然出现以下错误
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)
并在development.log中出现以下错误
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"
预先感谢
推荐答案
remote: true
不适用于分段上传/文件上传.
remote: true
does not work with multipart/file upload.
您可以为此使用远程宝石.
https://github.com/JangoSteve/remotipart/blob/master/README.rdoc
这篇关于remote:true选项不适用于rails 4中的文件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!