带有remote:true的form_tag不会发出Ajax请求 [英] form_tag with remote: true does not make an ajax request

查看:115
本文介绍了带有remote:true的form_tag不会发出Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有很多不同的表单,所有表单都使用remote: true.但是一个不能正常工作,因为它没有使用ajax调用.

I have many different forms in my app, all of them are using remote: true. But one does not work correctly, because is does not use an ajax call.

清理后,它看起来像:

<%= form_tag(upload_file_ajax_path, remote: true, multipart: true) do %>
  <%= file_field_tag(:file) %>
  <%= submit_tag("upload") %>
<%end%>

标签如下:

<form accept-charset="UTF-8" action="/mycontroller/upload_file_ajax" data-remote="true" enctype="multipart/form-data" method="post">
  <input name="utf8" type="hidden" value="✓">
  <input name="authenticity_token" type="hidden" value="1234"></div>
  <input id="file" name="file" type="file">
  <input name="commit" type="submit" value="upload">
</form>

以及路线条目:

post "mycontroller/upload_file_ajax", as: "upload_file_ajax"

但是在Chrome开发工具中检查调用时,标题显示为:

But checking the call in Chrome Dev Tools, the header says:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

与其他表格不同:

Accept:*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript

我将js文件添加到布局中

I added the js files to my layout

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>

即使我从application.js文件中删除了所有JS代码(//= require jquery ...除外),该表单也无法正常工作.

And even if I remove all my JS code out of the application.js file (except the //= require jquery...), the form is not working correctly.

我想念什么?

推荐答案

您不能使用AJAX进行文件上传.即是正常情况.

You can't use AJAX for file uploads. That is, normally.

有一个很棒的瑰宝, Remotipart ,但它将此功能添加到了您的远程表单中.

There is an awesome gem, Remotipart, that adds this functionality to your remote forms though.

gem 'remotipart', '~> 1.0'

在application.js中

In application.js

//= require jquery.remotipart

这篇关于带有remote:true的form_tag不会发出Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆